-
Code problem
Code:
#include<iostream.h>
int main()
{
char num1[25];
char num2[25];
cout<<"Enter -1 to signal end of number.\n";
cout<<"The number should be in the format of\n";
cout<<"M = 1000\n";
cout<<"D = 500\n";
cout<<"C = 100\n";
cout<<"L = 50\n";
cout<<"X = 10\n";
cout<<"V = 5\n";
cout<<"I = 1\n";
cout<<"Enter the FIRST number to be calculated.\n";
while(cin>>num1[i] && num1[i] == -1)
i++;
cout<<"Enter the SECOND number to be calculated.\n";
while(cin>>num2[j] && num2[j] == -1)
j++;
return 0;
}
First off this is my first post to this board and im fairly new to C++. i have been visiting occasionally and found some useful tips . But im stomped on this problem. After first 'number' is inputted and -1 is entered the program terminates and doesnt allow for the second number to be entered. Whats wrong?
-
Yet another member from Atlanta. ;) The problem is that cin leaves annoying stuff in the input stream, you have to get rid of it or the next call to cin will fail on it. Here is one possible solution:
Code:
#include<iostream.h>
int main()
{
int i = 0, j = 0;
char num1[25];
char num2[25];
cout<<"Enter -1 to signal end of number.\n";
cout<<"The number should be in the format of\n";
cout<<"M = 1000\n";
cout<<"D = 500\n";
cout<<"C = 100\n";
cout<<"L = 50\n";
cout<<"X = 10\n";
cout<<"V = 5\n";
cout<<"I = 1\n";
cout<<"Enter the FIRST number to be calculated.\n";
while(cin>>num1[i] && num1[i] == -1)
i++;
cin.ignore();
cout<<"Enter the SECOND number to be calculated.\n";
while(cin>>num2[j] && num2[j] == -1)
j++;
return 0;
}
-Prelude
-
here's a couple things i think might be wrong
first I don't see that you declare i or j ( but maybe you just didn't post them
second, the way you have the whiles setup with the && , i'm not sure that you can expect the compiler to do the left side of && first then the right side, and also I think some compilers will do the right side first and since that fails there is no need to check if the left side of && is true so therefor the left side never gets run.
I'm also not sure that -1 is just one char, but i'm not sure if that;s ok or not.
Also I think you just have to totally rethink those loops in general.