Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,268 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,475 people online right now. Registration is fast and FREE... Join Now!




loop to call function

2 Pages V  1 2 >  
Reply to this topicStart new topic

loop to call function, menu

mistic857
5 Nov, 2006 - 07:04 PM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
I can get the first program to come up, I understand that part. But where I am lost is how do I get the menu to come back up so you can pick another selection?
When I am done with all the functions I should have 5 different ones.
I know it is a loop, but I have no idea how to do the DO..
I attempted different loops and all I get is errors. Thought I had it was down to one error. But got one when linking..
so I took the do loop out completely ( mostly so I don't look like a fool for you guys)
If you can point me in the right direction I would be one happy camper!
CODE
#include <string>
#include <iostream>
#include <iomanip>
//#include (fstream>

using namespace std;

// declare my functions 1st
void main_menu(int &selection);
float program1();
//float program2();
//void exit();

// Global variables?
int selection;


int main() // beginning of main function

{// you must call your function here
    
    
    
        main_menu(selection);

    

   if (selection == 1)
    {
        program1();
    
    }
    else if(selection == 2)
    {
        cout <<"program";
    }
    else if(selection == 0)
    {
        return 0;
    }
    else
    {
        cout <<"Invaild entry" <<endl;
    }



return 0;

}
void main_menu(int &selection)

{
    cout << "*********************************************************" << endl;
    cout << "************           MENU                **************" << endl;
    cout << "************    Please make a selection    **************" << endl;
    cout << "************                               **************" << endl;
    cout << "*********************************************************" << endl;
    cout << "*********************************************************" << endl;
    cout << setiosflags(ios::fixed);
    cout << "  Option        "<< setw(25) << "Description" <<endl;
    cout <<" _______________________________________________________"  <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|   1.                Calculate sales tax on items      |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|   2.                Create a Customer contact card    |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|   3.                Balance a checkbook               |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|   4.                Calculate sales item from file    |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|   5.                Trip mileage Report               |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|   6.                Exit                              |" <<endl;
    cout <<"|                                                       |" <<endl;
    cout <<"|_______________________________________________________|" <<endl;
    cout <<"**********************************************************"<<endl;
    cout <<"Please make your selection: ";
    cin >> selection;
                  





}
float program1()
{

    // define var's

    float total_sales = 0;
    float sales_tax;
    float grand_total;
    float price;
    int num_items;
    int counter = 1;
    char answer;

    int x = 1;

    do
    {
    system("cls");
    counter = 1;
    total_sales= 0;


    // input
    cout <<"How many sale items do you have? : ";
    cin >> num_items;

    // while statement

        while(counter <= num_items)
        {
        cout <<"Enter the value of sales item" << counter << " : $";
        cin >> price;

        counter = counter + 1;
        total_sales = total_sales + price;

        }

    // output
    sales_tax = total_sales * .06;
    grand_total = sales_tax + total_sales;

    cout << endl;


    

    cout << "********************************************" << endl;
    cout << "********  S A L E S  R E C E I P T  ********" << endl;
    cout << "********************************************" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << setiosflags(ios::fixed) << setprecision(2);
    cout << "**  Total Sales           $" << setw(9) << total_sales << "      **" << endl;
    cout << "**  Sales tax             $" << setw(9) << total_sales * .06 << "      **" << endl;
    cout << "**                         -----------    **" << endl;
    cout << "**  Grand Total           $" << setw(9) << grand_total << "      **" << endl;
    cout << "**                                        **" << endl;
    cout << "**                                        **" << endl;
    cout << "********************************************" << endl;
    cout << endl;

    cout << "Would you like for this program to run again?" << endl;
    cout << "Please enter y or n."<< endl;
    cin >> answer;
    
    }
    while (answer == 'Y' || answer == 'y');

    

    

        
            

    return grand_total;

}


edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Xing
RE: Loop To Call Function
5 Nov, 2006 - 07:19 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Put your main menu in a while loop.
User is offlineProfile CardPM
+Quote Post

mistic857
RE: Loop To Call Function
5 Nov, 2006 - 07:27 PM
Post #3

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
QUOTE(Xing @ 5 Nov, 2006 - 08:19 PM) *

Put your main menu in a while loop.



and a do?
I get real confused with the loops. Not sure how, been working on this all day, I have attempted so many things.

User is offlineProfile CardPM
+Quote Post

mistic857
RE: Loop To Call Function
5 Nov, 2006 - 08:03 PM
Post #4

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
QUOTE(mistic857 @ 5 Nov, 2006 - 08:27 PM) *

QUOTE(Xing @ 5 Nov, 2006 - 08:19 PM) *

Put your main menu in a while loop.



and a do?
I get real confused with the loops. Not sure how, been working on this all day, I have attempted so many things.



I put a while in there and it still isn't working.
got down to 3 errors now....keeps telling me that about it being "void"
I really don't understant.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Loop To Call Function
5 Nov, 2006 - 08:05 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Post the error messages that you are getting.
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Loop To Call Function
5 Nov, 2006 - 08:33 PM
Post #6

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Your confusing us...

Post your error please...


we will help you with that



User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Loop To Call Function
6 Nov, 2006 - 01:02 AM
Post #7

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(mistic857 @ 5 Nov, 2006 - 08:04 PM) *

can get the first program to come up, I understand that part. But where I am lost is how do I get the menu to come back up so you can pick another selection?
When I am done with all the functions I should have 5 different ones.
I know it is a loop, but I have no idea how to do the DO..
I attempted different loops and all I get is errors. Thought I had it was down to one error. But got one when linking..
so I took the do loop out completely ( mostly so I don't look like a fool for you guys)
If you can point me in the right direction I would be one happy camper!

QUOTE
I put a while in there and it still isn't working.
got down to 3 errors now....keeps telling me that about it being "void"
I really don't understant

When the guys suggested a while or do loop they mean something like this....
CODE
do{

    main_menu(selection);

    

   if (selection == 1)
    {
        program1();
    
    }
    else if(selection == 2)
    {
        cout <<"program";
    }
    else if(selection == 0)
    {
        return 0;
    }
    else
    {
        cout <<"Invaild entry" <<endl;
    }

}while ( condition );

return 0;
}

I point out that the while ( condition ); must have a logical expression (ie evaluates to true or false) replace condition with something that will let the loop exit when the user wants to stop.
User is offlineProfile CardPM
+Quote Post

mistic857
RE: Loop To Call Function
6 Nov, 2006 - 05:35 AM
Post #8

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
Ok, I did the loop
and here is what I got now. This is the same error I got when I tried before.

cpp(68) : error C2062: type 'void' unexpected

Thanks!



User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Loop To Call Function
6 Nov, 2006 - 05:37 AM
Post #9

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(mistic857 @ 6 Nov, 2006 - 06:35 AM) *

Ok, I did the loop
and here is what I got now. This is the same error I got when I tried before.

cpp(68) : error C2062: type 'void' unexpected

Thanks!

Mistic

Line 68 (or somewhere above that) is the problem. Can you post lines 1 to 70 for me please smile.gif
User is offlineProfile CardPM
+Quote Post

mistic857
RE: Loop To Call Function
6 Nov, 2006 - 10:33 AM
Post #10

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
Hope this doesn't post twice now.
I thought I had posted but I guess I lost connection or something.

here is what I did....


//start

CODE

#include <string>
#include <iostream>
#include <iomanip>
//#include (fstream>

using namespace std;

// declare my functions 1st
void main_menu(int &selection);
float program1();
//float program2();
//void exit();

// Global variables?
int selection;


int main() // beginning of main function

{// you must call your function here
    
    int ans;

    
    //while(main_menu(selection));
    
    do{

    system("cls");
    
    

        cout <<"Would you like to pick another selection off the main menu? ";
        cin >> ans;

            main_menu(selection);
                
    

   if (selection == 1)
    {
        program1();
    
    }
    else if(selection == 2)
    {
        cout <<"program";
    }
    else if(selection == 0)
    {
        return 0;
    }
    else
    {
        cout <<"Invaild entry" <<endl;
    }
    while (ans == 'Y' || ans == 'y');
    }

return 0;

}
void main_menu(int &selection)
}



cpp(65) : error C2059: syntax error : 'return'

I don't understand "what" to call something. I think I understand the"how"..
I know if statement took awhile till I finally figured them out. Still have trouble with them.

edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

mistic857
RE: Loop To Call Function
6 Nov, 2006 - 10:42 AM
Post #11

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
Am I putting my loop in the wrong place?

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Loop To Call Function
6 Nov, 2006 - 05:20 PM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
The problem is the while part of your loop. It should be after the closing brace of the while loop. Not inside the loop as you currently have it.
CODE

        cout <<"Invaild entry" <<endl;
    }
    
    } while (ans == 'Y' || ans == 'y');

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 07:40PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month