Mac whats wrong here: c++ help

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
720
1
Texas
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
 

bousozoku

Moderator emeritus
Jun 25, 2002
13,951
3
Gone but not forgotten.
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++.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
720
1
Texas
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*
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

galdar496

macrumors newbie
Jul 15, 2007
3
0
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.
 

fimac

macrumors member
Jan 18, 2006
95
0
Finland
im trying to check to see if a user has entered an integer.
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;
}
 
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;
}