PDA

View Full Version : whats wrong here: c++ help




macman2790
Aug 29, 2007, 06:15 PM
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:

#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
Aug 29, 2007, 07:27 PM
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++.

toddburch
Aug 29, 2007, 07:31 PM
http://www.cppreference.com/stdstring/isdigit.html

Todd

macman2790
Aug 29, 2007, 09:21 PM
the cp pointer isn't working, im using the method c_str() to convert a string to a char array
#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
Aug 29, 2007, 10:11 PM
This is illegal syntax:

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

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
Aug 30, 2007, 02:03 AM
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
Aug 30, 2007, 06:02 AM
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

#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;
}

AlmostThere
Aug 30, 2007, 01:08 PM
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


#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;
}