|
-
August 14th, 2005, 05:21 AM
#1
Make program loop untill null value is passed.
Hi,
i've had to write this program below to take input in the form of "ONE-HUNDRED" and output "100", only i don't want it to exit straight away. I want it to keep taking inputs untill a null value is passed.
I tried using a do while loop but it doesn't take in inputs after the first one and keeps printing the current output to the screen.
Here is what i have so far.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char number[80];
char *str = NULL;
int output = 0;
cout << "Student ID 4095983" << endl;
cin.get( number, 79 );
str = strtok( number, " -" );
while ( str != NULL ) {
if( strcmp(str, "ZERO") == 0) {
output += 0;
}
else if( strcmp(str, "ONE") == 0) {
output += 1;
}
else if( strcmp(str, "TWO") == 0) {
output += 2;
}
else if( strcmp(str, "THREE") == 0) {
output += 3;
}
else if( strcmp(str, "FOUR") == 0) {
output += 4;
}
else if( strcmp(str, "FIVE") == 0) {
output += 5;
}
else if( strcmp(str, "SIX") == 0) {
output += 6;
}
else if( strcmp(str, "SEVEN") == 0) {
output += 7;
}
else if( strcmp(str, "EIGHT") == 0) {
output += 8;
}
else if( strcmp(str, "NINE") == 0) {
output += 9;
}
else if( strcmp(str, "TEN") == 0) {
output += 10;
}
else if( strcmp(str, "ELEVEN") == 0) {
output += 11;
}
else if( strcmp(str, "TWELVE") == 0) {
output += 12;
}
else if( strcmp(str, "THIRTEEN") == 0) {
output += 13;
}
else if( strcmp(str, "FOURTEEN") == 0) {
output += 14;
}
else if( strcmp(str, "FIFTEEN") == 0) {
output += 15;
}
else if( strcmp(str, "SIXTEEN") == 0) {
output += 16;
}
else if( strcmp(str, "SEVENTEEN") == 0) {
output += 17;
}
else if( strcmp(str, "EIGHTEEN") == 0) {
output += 18;
}
else if( strcmp(str, "NINETEEN") == 0) {
output += 19;
}
else if( strcmp(str, "TWENTY") == 0) {
output += 20;
}
else if( strcmp(str, "THIRTY") == 0) {
output += 30;
}
else if( strcmp(str, "FORTY") == 0) {
output += 40;
}
else if( strcmp(str, "FIFTY") == 0) {
output += 50;
}
else if( strcmp(str, "SIXTY") == 0) {
output += 60;
}
else if( strcmp(str, "SEVENTY") == 0) {
output += 70;
}
else if( strcmp(str, "EIGHTY") == 0) {
output += 80;
}
else if( strcmp(str, "NINETY") == 0) {
output += 90;
}
else if( strcmp(str, "HUNDRED") == 0) {
output *= 100;
}
else if( strcmp(str, "THOUSAND") == 0) {
output *= 1000;
}
else if( strcmp(str, "MILLION") == 0) {
output *= 1000000;
}
str = strtok( NULL, " -" );
}
cout << "Conversion to integer is: " << output << endl;
}
Any help appreciated,
thanks
-
August 14th, 2005, 06:17 AM
#2
Re: Make program loop untill null value is passed.
Hint: you need two loops, one inside the other.
Dave Mclelland.
-
August 14th, 2005, 07:30 AM
#3
Re: Make program loop untill null value is passed.
Another bigger hint - You need to have the cin inside the outer loop.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 14th, 2005, 09:16 AM
#4
Re: Make program loop untill null value is passed.
Yet another hint.
You have to get rid of a remaining '\n' from a previous iteration before using cin.get().
-
August 15th, 2005, 03:38 AM
#5
Re: Make program loop untill null value is passed.
One more hint: use std::map<string,whatever> instead of that long list of if(strcmp(...)==0).
"Programs must be written for people to read, and only incidentally for machines to execute."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|