Thread: Help with isdigit and for loops

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Angry Help with isdigit and for loops

    I am attempting to check whether a number entered is a digit. I have devised that the way to do this is to create a character array and save the user input there. Then I open/create a text file. USing a for loop I check that they are all digits. If they are my main program should continue on the return of the value 1 in "digit".



    It took me ages to get the open/write/read file to work and now my program does the loop but makes me input too many numbers and wont move on. The user must be able to input a figure of up to 3 digits... If someone could help I would be extremely grateful... I may even send flowers....


    Code:
    int coincheck(void)
    
    {
       ofstream outfile("c:\data.txt",ios::out); //creates outfile
       if(!outfile)
         {
            cerr<<"error:cant open\n";
            exit(1);
          }
        //cout<<"please enter your coin.\n";
        cin>>coin;
        cout<<"\n!!! - "<<coin;
        do{
                for (i=0;(c=getchar()) != '\n';++i)
                   {
                       coin[i] = c;
                       if(isdigit(c))
                            {
                                 cout<<"gifgvhcfj";
                                 outfile<<c;
                                 digit=1;
                             }
                       else
                            {
                                 cout<<"must be a number/n";
                                 digit=0;
                                 exit(1);
                            }
                     }
          outfile.close();
         }while (coin !=0);
       ifstream infile("c\data.txt",ios::in); //opens file
       if(!outfile)
          {
              cerr<<"error:cant open\n";
              exit(1);
           }
        infile>>newcoin;
        infile.close();
        return (0);
        exit(1);
        }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    while (coin !=0);
    When will that ever happen?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    3
    Sorry... What I should have told you is that this is a function in a much bigger program that I am writing. The user has to enter on of 5 coin choices 5, 10, 20, 50, 100. To terminate this loop they are to simply enter 0. I am okay checking the values in my main with an if statement using a lot of OR's but decided to write a function to check if it was a digit to stop people inputting letters. Another problem I have is with my for loop... it goes on for too long if they just want to enter the number 5

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yeah, but coin is a pointer to a string. Why would it ever be 0?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    3
    OKAY!!! So how do I say "While the user has not entered the value '0' "? Coin was an int before I changed it to a pointer so I thought it would work... I thought wrong

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Off the top of my head:
    Code:
    while(std::cin.get() != '0')

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > ofstream outfile("c:\data.txt",ios:ut);
    Don't forget, to get a single \, you must write \\

    > ifstream infile("c\data.txt",ios::in);
    Missing : as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed