whats wrong here: c++ help

Discussion in 'Mac Programming' started by macman2790, Aug 29, 2007.

  1. macrumors 6502a

    macman2790

    #1
    im trying to check to see if a user has entered an integer. But everything im doing, even if its all letters it turns out that it says its an integer.

    heres the source:
    Code:
    
    #include <iostream>
    #include <string>
    #include <cstdio>
    using namespace std;
    using std::string;
    
    void menu(), enter();
    int main () {
    	enter();
       
        return 0;
    }
    // promps a user to type an integer and then press enter to check if it is an integer numeral.
    void enter(){
    	char *x;
    
    	int a;
    	do {
    	cout << "Press either a digit 0-9, or '-' to enter a character.\n";
    	cout << "Press [return] to finish entering characters:";
    	cin >> x;
    	string s = x;
    	if(!s.find("."))
    	a = atoi(x);
    	
    	cout << '\n';
    	if (!a)
    		cout << "You did not enter an integer numeral.";
    	}while (!a);
    	cout << "Success, you entered an integer numeral!";
    }
    
    
    
    thanks in advance
     
  2. Moderator emeritus

    #2
    Characters are integers.

    I would suggest that you use something like void isnum(char c) that we use in C. Its header is ctype.h, but I'm not sure whether there is such a function in C++.
     
  3. macrumors 6502a

  4. macrumors 6502a

    macman2790

    #4
    i found a good way, but im not sure why this pointer isnt working

    the cp pointer isn't working, im using the method c_str() to convert a string to a char array
    Code:
    #include <iostream>
    #include <string>
    #include <cstdio>
    
    using namespace std;
    using std::string;
    
    void menu(), enter();
    bool checkNumeral();
    int main () {
    	enter();
       
        return 0;
    }
    // promps a user to type an integer and then press enter to check if it is an integer numeral.
    void enter(){
    	
    	char *cp = new char[80];
    	string s;
    	int a;
    	do {
    	cout << "Press either a digit 0-9, or '-' to enter a character.\n";
    	cout << "Press [return] to finish entering characters:";
    	cin >> s;
    	
    	cp = s.c_str();
    	if (s.find(".") == string::npos)
    		a = atoi(cp);
    		cout << "You did not enter an integer numeral.";
    	}while (!a);
    	cout << "Success, you entered an integer numeral!";
    }
    i get invalid conversion from const char* to char*
     
  5. macrumors 6502a

    #5
    This is illegal syntax:
    Code:
    char chars[80] ; 
    chars = "macman" ; 
    
    char chars[80] ; is C notation when you want to work with a null terminated string.

    And, then you have
    Code:
    string s ; 
    
    which is a C++ String class definition.

    You can't simply use the = sign to assign a string class object to a character array. There are some popular C functions that allow you to do this though.

    Todd
     
  6. macrumors newbie

    #6
    you could simply cast that input as an int and see if the value is in the correct range (in your case 1 to 9). In other words, if you read like this: cin>> x;
    you could then check x by saying if((int)x > 0 && (int)x < 9), because a character that is cast to an int will be outside of that range.
     
  7. macrumors member

    #7
    As others have noted, use the isdigit function. The following code does what you want. Handling negative numbers and exponents is left as an exercise for the reader :D

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
      bool is_valid = false;
    
      while (!is_valid)
        {
          cout << "Enter a number using digits 0-9: ";
    
          char x[80];  /* FIXME: stack overflow if user enters a long string */
          cin >> x;
    
          for (char* p = x; *p; ++p)
            {
              if (!isdigit(*p))
                {
                  is_valid = false;
                  break;
                }
              is_valid = true;
            }
        }
    
      cout << "Success";
    
      return 0;
    }
     
  8. macrumors 6502a

    #8
    If you are doing this in c++ (and depending on the goal / purpose, I am assuming from you code you just want to read an integer) you should be using a stringstream

    Code:
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        string buffer;
        
        cout << "Enter a number" << endl;
        getline(cin, buffer);
        cout << "Line entered was " << buffer << endl;
        
        istringstream iss(buffer);
        int i;
        if (iss >> i)
            cout << "Number entered was " << i << endl; 
        else
            cout << "No number entered" << endl;
    }
    
     

Share This Page