C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 28th, 2003, 11:21 AM
geek1099 geek1099 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 5 geek1099 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to geek1099
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

Reply With Quote
  #2  
Old May 1st, 2003, 10:45 AM
empvision empvision is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Czech republic
Posts: 1 empvision User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 : URL

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > while statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway