Jump to content
  • Advertisement
Sign in to follow this  
Tobbe

weird C++ thingy

This topic is 5147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
dont forget : #include


#include

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


neyna

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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 = true
loop

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!