Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My main menu runs twice after displaying my ascending and descending order function,
i have to scroll up in order to see.
how can i solve that? i don wan the main menu to come up.
here are the codes for function

C++
int main(int argc, char *argv[]);  // ascending and descending function 
{
    std::ifstream input_file("stockdatabase.txt"); // text file
  std::string value;
  std::vector<std::string> file_data;
   if (input_file)
  {
    while (!input_file.eof())
    {
      std::getline(input_file, value, '\n');
      file_data.push_back(value);
    }
  }
    int choice = 0;
    do
    {
        cout<< "Press 1 for descending order"<<endl;
        cout<< "Press 2 for ascending order"<<endl;
        cout<< "Press 3 to exit"<<endl;
        cin >> choice; //input choice 1 or 2
        switch (choice)
        {
        case 1:
            std::cout << "Sorting in Descending Order: \n";
            std::sort(file_data.begin(), file_data.end(), DecsendingOrderSorter());  //descending
  // now file_data is sorted.
  std::for_each(file_data.begin(), file_data.end(), RowPrinter());

            break;
        
        case 2:
            std::cout << "Sorting in Ascending Order: \n";
            std::sort(file_data.begin(), file_data.end(), AscendingOrderSorter()); // ascending
  // now file_data is sorted.
  std::for_each(file_data.begin(), file_data.end(), RowPrinter());
            break;
    
        case 3:
              system("clear");
                          exitSubMenu = true;
                        break;
                        
        default:
            // Invalid inputs will be handled here
            cout << "You have entered an invalid option, please key in Options 1 ~ 3. Thank you." << endl << endl;
            choice = -1;
                system("clear");
            break;
        }
    } while (choice != 3);
}
}



ERRORS
----------------------------------------
- Ascending and Descending Order -
----------------------------------------

S/N ItemID Total Qty Item Description
----------------------------------------------------------------------
Press 1 for descending order
Press 2 for ascending order
Press 3 to exit
3

(HERE I ALREADY PRESS 3 TO EXIT)

--------------------------------------------------------------------
- Welcome to Warehouse Management System -
--------------------------------------------------------------------

Login successful! Welcome back Henry Loh!

1) Add new stock
2) Remove stock
3) Edit stock item
4) Search stock item
5) List stock items in ascending or descending order
6) Daily stock summary report
7) Weekly stock summary report
8) Monthly stock summary report
9) Yearly stock summary report
10) Quit Program

Please enter your choice:

THIS IS THE MAIN MENU^
----------------------------------------
- Ascending and Descending Order -
----------------------------------------

S/N ItemID Total Qty Item Description
----------------------------------------------------------------------
Press 1 for descending order
Press 2 for ascending order
Press 3 to exit
OVERWRITTEN BY THIS^
Posted
Updated 23-Oct-13 6:44am
v5
Comments
OriginalGriff 23-Oct-13 11:30am    
I'm not even going to try and read that until it is indented and tidied up properly.
Use the "Improve question" widget to edit your question and make it easier to read. Help us to help you!
Member 10334475 23-Oct-13 11:39am    
how is that?
OriginalGriff 23-Oct-13 11:42am    
Better, but rather odd.
Out of interest, what do you think a switch block does? Do you need the if tests as well? If so, why?
Then just finish sorting out the indents...the top half is ok, but the bottom is still all over the place!
Member 10334475 23-Oct-13 11:49am    
i didnt have the if at first.
i just put it in to see whether it will correct my output but it doesnt
im a newbie. can teach me pls.
this ascending function outputs correctly but it follows by 2 calls of mainmenu which i duno why and eventually my ascending function couldnt be seen in the console.

Start with something simple like:
C++
int main(int argc, char *argv[]);  // ascending and descending function 
{
    int choice = 0;
    do
    {
        cout<< "Press 1 for descending order"<<endl;
        cout<< "Press 2 for ascending order"<<endl;
        cout<< "Press 3 to exit"<<endl;
        cin >> choice; //input choice 1 or 2
        switch (choice)
        {
        case 1:
            std::cout << "Sorting in Descending Order: \n";
            break;
        
        case 2:
            std::cout << "Sorting in Ascending Order: \n";
            break;
    
        case 3:
            break;
        default:
            // Invalid inputs will be handled here
            cout << "You have entered an invalid option, please key in Options 1 ~ 3. Thank you." << endl << endl;
            choice = -1;
            break;
        }
    } while (choice != 3);
}

Run that a few times to see how it works. Step through it with your debugger to see what changes at each step. Then gradually add the other pieces of code a step at a time until you have implemented all the pieces.
 
Share this answer
 
Comments
Member 10334475 23-Oct-13 12:23pm    
thank you. it worked for choice 1 and 2. but now the problem came to choice 3
Richard MacCutchan 23-Oct-13 12:34pm    
Well that code works exactly as it should for me; I wonder what you have changed, or are doing differently?
Member 10334475 23-Oct-13 12:36pm    
i have edited the code and pasted it. can help me take a look.
the choice 3 which is suppose to exit to main menu doesnt work properly.
it still stays at ascending and descending page after 3 is press
Richard MacCutchan 23-Oct-13 12:37pm    
Then you have changed something, but unless you show us exactly what you are running we cannot guess what it is.
Member 10334475 23-Oct-13 12:44pm    
i pasted the errors too take a look
OK. So... take out the if stuff - you don't need it, and it just confuses the issue. Then in VS press CTRL+A then CTRL+E,CTRL+F to reformat the whole document - this should fix the indentation and make your code more readable.

Next, you need to look at your code...which is a bit odd, and having the indentation fixed will probably highlight that. Does this even compile? Given that you are declaring the prototype of the Main function in the middle of the SortDescending constructor...

Assuming it does, start off by putting a breakpoint on the first line of code, and run your app.
When it hits the breakpoint, single step through each line in turn, but work out before you run teh line what you expect to happen. Does it? Or does something else happen? Why?

See how far you get before it is obvious that what you thought should happen didn't.

This may seem cruel, but this is a skill (honest - it's called basic debugging) and it is pretty much essential that you learn it before you go much further.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900