|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 : URL |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > while statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|