Problems with loops

This is a discussion on Problems with loops within the C++ Programming forums, part of the General Programming Boards category; can anyone tell me what is wrong with my code? #include <iostream> #include <iomanip> #include <cstring> using namespace std; void ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    Problems with loops

    can anyone tell me what is wrong with my code?

    #include <iostream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    void input();
    void compute();
    void print();
    char name[6][21];
    float previous_balance [6];
    float payments [6];
    float charges [6];
    void main()
    {
    input();
    }

    void input()
    {
    for (int counter=0; counter<6; counter++)
    {
    cout<<"Enter the last name for the "<<counter+1<<" customer."<<endl;
    cin>>name[counter];
    cout<<"Enter the previous balance for customer "<<name[counter]<<endl;
    cin>>previous_balance [counter];
    if (isdigit(previous_balance[counter])==0)
    {
    cout<<"Cannot contain letters."<<endl;
    counter--;
    }

    }
    }

    sorry that the code looks abit messy. What I do not understand is, why it give me this when I typed a letter for cin>>previous_balance

    Enter the last name for the ... customer
    .
    .
    .
    .
    .
    it will just flood the screen and keep repeating the same sentence above. I know that previous balance is a float, but i am just checking it to make sure it is really entered as a float. I have tried putting cin.get(): before every cin. However, it still give me the same problem.
    Last edited by Mingzhi; 06-26-2003 at 08:59 AM.

  2. #2
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    I'm not sure why it keeps repeating that sentence, but I can tell you one error in the code: you declared previous_balance to be of type float. That means it cannot hold letters. When you type a letter in the input and it tries to store the letter in previous_balance in the line:

    cin>>previous_balance [counter];

    that will create an error! If you want the user to be able to enter letters without the program crashing, you'll need to store the input in a string or char[] first, then check to see if it's a number before converting it to a float.

    I don't know if that will fix your problem or not, but it should help!
    My programs don't have bugs, they just develop random features.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    a) Use code tags. On the top of each board here, there is a sticky on how to use them. Makes your code look nicer when you post it.

    b) Do NOT use void main(). Use int main() and return 0. Why? Read the FAQ.

    c) You're not supposed to put in letters when its expecting a number. It will screw things up.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Problems with loops

    Originally posted by Mingzhi
    What I do not understand is, why it give me this when I typed a letter for cin>>previous_balance

    Enter the last name for the ... customer
    .
    .
    .
    .
    .
    it will just flood the screen and keep repeating the same sentence above. I know that previous balance is a float, but i am just checking it to make sure it is really entered as a float. I have tried putting cin.get(): before every cin. However, it still give me the same problem.
    If you try to read an invalid input (e.g. a letter when expecting a number) the stream goes into an error state; the number you get back is undefined. You need to check for the error state and recover from it before you can do anything else.

    Once a stream errors, until the error is cleared, you can't read ANYTHING from it.
    Last edited by Cat; 06-26-2003 at 11:47 AM.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    Thank you so much for replying back to my queries. However, I am still a newbie lor & do not know how to convert data in a char to a floating point number. Need to learn more. Anyway, thanks for answering my queries

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 09:04 PM
  2. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 12:30 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  4. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21