Thread: while statement

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    5
    Rep Power
    0

    while statement


    I have typed up a program to have a loop to let the user do a math function or enter a name function. I want this loop to be continuios untill the user exits the program. I did not put the code in to exit the program because I am more concern about the loop working. Here is the code that I have typed up:

    #include <iostream.h>

    int info();
    void doMath();
    void enterName();

    int main()
    {
    int x;
    x=info();
    cin.ignore();
    while(x==1)
    {
    doMath();
    x=info();
    cin.ignore();
    }
    while(x==2)
    {
    enterName();
    x=info();
    cin.ignore();
    }
    return 0;
    }

    int info()
    {
    char y;
    cin.ignore();
    cout << "\nPress M to do the math" << "\nPress N to enter the name";
    cout << "\nWhat would you like to do? ";
    y=cin.get();
    if(y == 'M' || y == 'm')
    return 1;
    if(y == 'N' || y == 'n')
    return 2;
    else
    return 0;
    }


    void doMath()
    {
    int hour,wage,pay;
    cout << "\nEnter the hours worked";
    cin >> hour;
    cout << "\nEnter the wage";
    cin >> wage;
    pay = hour*wage;
    cout << "\nThe pay is " << pay;
    }

    void enterName()
    {
    char name[10];
    cout << "\nEnter the name";
    cin.getline(name,20);
    cin.ignore();
    cout << "\nYou entered: " << name;
    }

    When I run the program it will not let me enter my choice of doing the math or entering the name
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Czech republic
    Posts
    1
    Rep Power
    0

    Exclamation


    Try this!!!

    // while.cpp
    #include <iostream.h>
    #include <cctype>
    using namespace std;

    // user function
    char info();
    void doMath();
    void enterName();

    int main()
    {
    char sign;
    sign = toupper(info());
    while (sign == 'M' || sign == 'N')
    {
    cin.clear();
    cin.get();
    switch (sign)
    {
    case 'M': doMath();
    case 'N': enterName();
    }
    sign = toupper(info());
    }
    cout << "Bye bye.\n";
    return 0;
    }

    char info()
    {
    char y;
    cout << "\nPress M to do the math" << "\nPress N to enter the name";
    cout << "\nWhat would you like to do? ";
    cin >> y;
    return y;
    }
    void doMath()
    {
    int hour,wage,pay;
    cout << "\nEnter the hours worked: ";
    cin >> hour;
    cout << "Enter the wage: ";
    cin >> wage;
    pay = hour * wage;
    cout << "The pay is " << pay << "\n";
    }
    void enterName()
    {
    char name[50];
    cout << "\nEnter the name: ";
    cin.getline(name,50);
    cout << "You entered: " << name << "\n";
    }

    if it doesn't work, mail me : master1@centrum.cz

IMN logo majestic logo threadwatch logo seochat tools logo