reading more than one char

This is a discussion on reading more than one char within the C++ Programming forums, part of the General Programming Boards category; I've written a claculator program in C++, at the moment it prompts for two numbers (which are read as char's), ...

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

    reading more than one char

    I've written a claculator program in C++, at the moment it prompts for two numbers (which are read as char's), if they aren't numbers it loops until the user inputs a number. The problem I have now, is that only one character can be inputted at a time. Does anybody know how I can rectify this so the first prompt can take as many char as possible as well as the second prompt taking in more than one char, instead of each prompt only accepting one input? I think it needs an array for each variable but each time I try I get a few errors.

    Below is the code I have written so far (minus the case statements that go through the operaters that add, subtract etc.):
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    double add(double num1, double num2)        
    {
        return num1 + num2;
    }
    
    double subtract(double num1, double num2)
    {
        return num1 - num2;
    }
    
    double divide(double num1, double num2)
    {
        return num1 / num2;
    }
    
    double multiply(double num1, double num2)
    {
        return num1 * num2;
    }
    
    //Main function
    int main()        
    {
        //Variable declarations
        char first_input;        
        char second_input;
        double num1;
        double num2;
        char op;            
        bool valid;
    
        cout << "*****************************" << endl;
        cout << "**        calculator       **" << endl;
        cout << "*****************************" << endl;
            
        //Do-while loop testing user input
        do 
        {
            cout << "Please enter a number: ";            
            cin >> first_input;
    
            valid = true;
            valid = (first_input >= '0' && first_input <= '9');
    
            if(valid){
                cout << "Correct data!\n";
                num1 = atof(&first_input);}
            else
                cout << "In-correct data\n";
        }while(!valid);
    
    
        //Do-while loop testing user input
        do 
        {
            cout << "Please enter a number: ";            
            cin >> second_input;
    
            valid = true;
            valid = (second_input >= '0' && second_input <= '9');
            
            if(valid){
                cout << "Correct data!\n";
                num2 = atof(&second_input);
                }
            else
                cout << "In-correct data\n";
        }while(!valid);


    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    You seem to be quite new to programming. There exists a certain data type called a string, which is really nothing but a char array. If you know about arrays then use gets() to input the string, and include stdio.h(you can also use cin to input the string). You can then test the string to see that only digits, the - sign and the decimal point are entered. Otherwise, you can use cin for inputting the numbers directly. Do not try to jump ahead.

    Compiler : Turbo C++ v1.01

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is one solution.

    std::getline(cin, stringObject);

    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Sussed it.

    Cheers for your help, I went with the string and that worked well enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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