Thread: Code problem

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    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?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM