Thread: Need help finding bug

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Need help finding bug

    hi,

    i cant seem to detect the bug in this program. after iv'e enter my information, the programs just crashes as in a bunch of garbage prints endlessly onto the screen.


    Code:
    #include <iostream> 
    using namespace std; 
    
    char name[10][80];  // this array holds employee names 
    char phone[10][20]; // their phone numbers 
    float hours[10];    // hours worked per week 
    float wage[10];     // wage 
    
    int menu(); 
    void enter(), report(); 
    
    int main() 
    { 
      int choice; 
    
      do { 
        choice = menu(); // get selection 
        switch(choice) { 
          case 0: break; 
          case 1: enter(); 
            break; 
          case 2: report(); 
            break; 
          default: cout << "Try again.\n\n"; 
        } 
      } while(choice != 0); 
    
      return 0; 
    } 
    
    // Return a user's selection. 
    int menu() 
    { 
      int choice; 
    
      cout << "0. Quit\n"; 
      cout << "1. Enter information\n"; 
      cout << "2. Report information\n"; 
      cout << "\nChoose one: "; 
      cin >> choice; 
    
      return choice; 
    } 
    
    // Enter information. 
    void enter() 
    { 
      int i; 
      char temp[80]; 
    
      for(i=0; i<10; i++) { 
        cout << "Enter last name: "; 
        cin >> name[i]; 
        cout << "Enter phone number: "; 
        cin >> phone[i]; 
        cout << "Enter number of hours worked: "; 
        cin >> hours[i]; 
        cout << "Enter wage: "; 
        cin >> wage[i]; 
      } 
    } 
    
    // Display report. 
    void report() 
    { 
      int i; 
    
      for(i=0; i<10; i++) { 
        cout << name[i] << ' ' << phone[i] << '\n'; 
        cout << "Pay for the week: " << wage[i] * hours[i]; 
        cout << '\n'; 
      } 
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I tried your code and the only time I was able to get the problem you described was when I tried to type 1,3 in one of the floats (or any other decimal number using , to point out its a decimal), try to use . when you write in decimal numbers.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need to eat the newline character after the wage is entered. To do this, add an ignore():

    cout << "Enter wage: ";
    cin >> wage[i];
    cin.ignore(std::numeric_limits < int >::max(), '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  3. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  4. help finding a bug
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-22-2005, 02:15 AM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM