Join 136,564 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,895 people online right now. Registration is fast and FREE... Join Now!
using namespace std; char* get_num_month(int month);
int main()
{ int mm=0; char ch = 0; char *month; while(ch != 'q') // to quit { cout << "Enter month number I will display it in string: "; cin >> mm; cout << "Enter another number or press the q-key to quit: "; cin >> ch;
if( mm < 1 || mm > 12 )// Validate to see if input for month is valid { cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl; } } //cin.get(); return 0; }
Enter month number I will display it in string: 3 Enter another number or press the q-key to quit: 2 The month you entered: 3 is: March Enter month number I will display it in string:
All I want it do is this:
Enter month number I will display it in string: 3 The month you entered: 3 is: March Enter another number or press the q-key to quit: q
I can not see what I did wrong, can anyone help me see what I did wrong, please help.
int main(void) { int mm=0; char ch = 0; char *month; while(ch != 'q') { // q to quit cout << "Enter month number I will display it in string: "; cin >> mm;
if( mm < 1 || mm > 12 ) { // Validate to see if input for month is valid cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm); cout << "The month you entered: " << mm << " is :" << month << endl; } cout << "Enter another number or press the q-key to quit: "; cin >> ch; } return 0; }
Thank you very much for your reply, this is what I got after I make changes:
Enter month number I will display it in string: 3 The month you entered: 3 is: March Enter another number or press the q-key to quit: 2 Enter month number I will display it in string:
Guys I got it working, I move the line cout << "Enter month number I will display it in string : " <<; put it above pass the while loop, that way it will not got call everytime, see the code below how I did it.
using namespace std; char* get_num_month(int month);
int main()
{ int mm = 0; char ch = 0; char *month;
cout << "Enter month number I will display it in string: "; cin >> mm;
while(ch != 'q') // to quit {
if( mm < 1 || mm > 12 )// Validate to see if input for month is valid { cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl; } cout << "Enter another number or press the q-key to quit: "; cin >> ch; } return 0; }
QUOTE(no2pencil @ 8 Apr, 2008 - 09:43 PM)
If I understand your question correctly...
cpp
int main(void) { int mm=0; char ch = 0; char *month; while(ch != 'q') { // q to quit cout << "Enter month number I will display it in string: "; cin >> mm;
if( mm < 1 || mm > 12 ) { // Validate to see if input for month is valid cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm); cout << "The month you entered: " << mm << " is :" << month << endl; } cout << "Enter another number or press the q-key to quit: "; cin >> ch; } return 0; }
I spoke to soon, it still di not work right, the quit part work ok if I press q-key it quit, but if I enter another number I got:
Enter month number I will display it in string: 3 The month you entered: 3 is: March Enter another number or press the q-key to quit: 2 Enter month number I will display it in string:
QUOTE(no2pencil @ 8 Apr, 2008 - 09:43 PM)
If I understand your question correctly...
cpp
int main(void) { int mm=0; char ch = 0; char *month; while(ch != 'q') { // q to quit cout << "Enter month number I will display it in string: "; cin >> mm;
if( mm < 1 || mm > 12 ) { // Validate to see if input for month is valid cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm); cout << "The month you entered: " << mm << " is :" << month << endl; } cout << "Enter another number or press the q-key to quit: "; cin >> ch; } return 0; }
Because the any input after the initial one is "ch" while the first is "mm" The selection structure does not account for "ch". I'll post some code in a sec, I'm on my laptop--hard to type.
I have changed the structure of the code, I declared a function with 2 parameters and I never use it. Now I have 2 error: error C2065: 'mm' : undeclared identifier in the main.cpp file (n_chars(arr,mm) and error C2447: '{' : missing function header (old-style formal list?) in the FunctionCall.cpp file I have seen this one before and it is seem that I missing ({}) somewhere in the code, but I can't see it.
CODE
This is Function prototype void n_chars(char arr, int mm);// Function proto type
This main.cpp file #include <iostream> #include <string> #include "GreenFunction.h" using namespace std;
And this is the functionCall.cpp #include <iostream> #include "GreenFunction.h" using namespace std; void n_chars(char *arr, int *mm);
{ int mm; char ch;
while(ch != 'q')// q to quit { cout << "Enter month number I will display it in string: "; cin >> mm;
if( mm < 1 || mm > 12 )// Validate to see if input for month is valid { cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm); cout << "The month you entered: " << mm << " is :" << month << endl; } cout << "Enter another number or press the q-key to quit: "; cin >> ch; }
return arr[month-1]; }
QUOTE(KYA @ 9 Apr, 2008 - 12:00 AM)
Because the any input after the initial one is "ch" while the first is "mm" The selection structure does not account for "ch". I'll post some code in a sec, I'm on my laptop--hard to type.
Use your old code and just have it ask if the user wants to input another number. Then have it loop again asking for a new month #. This solves your problem without creating any new ones.
Thank you for your help, I did try what I have below and when user enter " no " it work well, but when user enter "yes" this is what I got, perhapes my loop is in the wrong palce??
using namespace std; char* get_num_month(int month);
int main()
{ int mm = 0; char ch = 0; char *month;
cout << "Enter month number I will display it in string: "; cin >> mm;
if( mm < 1 || mm > 12 )// Validate to see if input for month is valid { cout << "INVALID month: Please try again." << endl; cin.get(); } else { month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl; cout << "Do you wish to enter another number? <y/n> "; cin >> ch; cin.get(); if(ch == 'y' || ch == 'Y') cout << "The month you entered: " << mm << " is :" << month << endl; else if(ch== 'n' || ch == 'N') cout << "Good bye and have a wonderfull day!" << endl; }
cin.get(); return 0; }
Enter month number I will display it in string: 3 The month you entered: 3 is: March Do you wish to enter another number? <y/n> n The month you entered: 3 is: March
If I have it loop again then I have to do that 12 times, then I have to adds this part 12 times for the total of 12 months:
CODE
month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl; cout << "Do you wish to enter another number? <y/n> "; cin >> ch; cin.get(); if(ch == 'y' || ch == 'Y') { cout << "Enter another month number: "; cin >> mm; month = get_num_month(mm); cout << "The month you entered: " << mm << " is :" << month << endl; cin.get(); }
else if(ch== 'n' || ch == 'N') cout << "Good bye and have a wonderfull day!" << endl;
QUOTE(KYA @ 9 Apr, 2008 - 03:50 AM)
Use your old code and just have it ask if the user wants to input another number. Then have it loop again asking for a new month #. This solves your problem without creating any new ones.
This post has been edited by KMH: 9 Apr, 2008 - 09:23 PM