weird C++ thingy

Started by
20 comments, last by Zahlman 19 years, 7 months ago
Ok guys, i need a code that will make the user input something and input it until the user either enters a letter or a negative number. And it must be able to handle values like 100000 and such. so how do i do this? this is what i have:

#include <iostream>
#include <cctype>

using namespace std; //I know it's nooby, but i dont care atm.

int main(void)
{
    char temp;
    int value;
    cout << "Write a value: ";
    cin >> temp;

    while (isdigit(temp) && temp >= 0)
    {
        value = temp;
        cout << "The value is " << value;
        cout << "Write a new value: ";
        cin >> temp;
    }

    cout << "Press any key to continue.";
    cin.get();
    cin.get();
    return 0;
}


edit : oops i forgot to say some things, but the thing is that char can't handle big amounts, i think CHAR_MAX on my comp is 127 or something, and int can't handle letters. So how do i fix this? Should i use some sort of array or something? :s
Advertisement
Look at the gets functions. (gets, fgets, ...)
and use a buffer : char buffer[128] for exemple.
atoi is a function to convert a string to an integer.

hope that helps.

neyna
dont forget : #include


#include

int main() {
char buffer[128];
while( strcmp(gets(buffer),"quit")!=0 ) {
printf("You wrote : %s\n",buffer);
}
return 0;
}


neyna
off the top of my head, might not be syntaticaly correct but
i hope you get the idea, if not, query me back.

#include <cstdio>#include <iostream>using namespace std;bool isNumber(const char *str){        // may need to trim str for leading/trailing whitespace	int start = 0;	if( str[0] == '-' ) // handle negatives too		start++;	for(int i=start; i<strlen(str); i++)		if( !isdigit(str) ) 			return false;	return true;}int main(){	bool done = false;	char temp[40] = {0}; // 40 chars should be enough	int ival = 0;	while( !done )	{        cout << "The value is " << temp;        cout << "Write a new value: ";        cin >> temp;		bool isNum = isNumber(temp);		if( isNum )			ival = atoi(temp);				if( isNum && ival < 0 || !isNum ) // if its negative OR a string			done = true;	}}


PS. i hope that wasnt a homework assignment, because if it was
your cheating nobody but yourself.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
I dont really understand, how would the code be?
edit: i only saw the first post after mine. you guys are fast at typing. thanks all :d
Something's weird today. (authentification problems)

i's like to write : #include < stdio.h >
Quote:Original post by Tobbe
I dont really understand, how would the code be?


#include <stdio.h>int main() {  char buffer[128];  int number=0;  while( strcmp(gets(buffer),"quit")!=0 ) {    printf("You wrote : %s\n",buffer);    number=atoi(buffer);    // ..........   }  return 0;}


hope that helps.

neyna
ok thank you all. I just thought that it might be an easier or obvious way to do it. But i will try to construct a longer function instead.
And it's not homework because i dont have programming in school :(

Btw, what's the difference between this:
char blabla = 5;int blablabla = atoi(blabla);

and this:
char blabla = 5;int blablabla = (int) blabla;
quick explination of my code example

while done is false     get user input     test if it is numberic           if( numeric )         convert to integer value     if( numeric and less than zero OR is not numeric )         set done = trueloop
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
atoi takes a const char* argument, and does a char by char
conversion (so its guaranteed to be numeric)

casting from a char to an int works but only if the char
is a digit (between '0' and '9') this is because ASCII digits
map directly to their values BUT letters etc. do not.

eg. try this
char a = '3';int val = (int) a;cout << val << endl;------------------------------char a = 'z';int val = (int) a;cout << val << endl;
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement