Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,936 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,438 people online right now. Registration is fast and FREE... Join Now!




Atoi function

 
Reply to this topicStart new topic

Atoi function, My program does not works properly

c--
31 Jan, 2008 - 07:20 PM
Post #1

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 27

Hello,

I try this code on my compiler, and it doesn't works. But I have tried that same dang code in other application, and it works fine. Does someone knows what the heck is going on with it?

Thanks in advance. smile.gif

This program asks a number, and if it's an invalid number will show a message. it converts a string to a integer.

CODE

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {


    char age[10];

    int intr = atoi ( age );

cout << "How old are you?" << endl;
cin >> age;

//If the user typed an invalid number or a negative number shows a message
if ( age[0] == 0 ){
    cout << "\a" << "You typed an invalid number" << endl << endl;}

if ( age[0] <= '0' )   {
    cout << "\a" << "You typed an invalid number" << endl << endl;}

else
{
    
    cout << "You have " << intr << " years old\n\n";
}


//Prevents the program to close after the user types his/her age
system("PAUSE");

return 0;
}



The problem is that my "if" function is not taking any action (if (age[0] == 0).
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Atoi Function
31 Jan, 2008 - 07:27 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
CODE

if ( age[0] == 0 ){
    cout << "\a" << "You typed an invalid number" << endl << endl;}

if ( age[0] <= '0' )   {


Your 1st if, is comparing to an int, the 2nd is comparing to a char... You can't do both.
User is offlineProfile CardPM
+Quote Post

teacher_mus
RE: Atoi Function
31 Jan, 2008 - 11:05 PM
Post #3

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 7

Hi,

your atoi should come after you take input from user. like this


CODE


char age[10];
cout << "How old are you?" << endl;
cin >> age;

int intr = atoi ( age );

//If the user typed an invalid number or a negative number shows a message
if ( intr == 0 ){
    cout << "\a" << "You typed an invalid number" << endl << endl;}

if ( intr <= 0 )   {
    cout << "\a" << "You typed an invalid number" << endl << endl;}

User is offlineProfile CardPM
+Quote Post

Bench
RE: Atoi Function
1 Feb, 2008 - 04:55 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(no2pencil @ 1 Feb, 2008 - 03:27 AM) *

Your 1st if, is comparing to an int, the 2nd is comparing to a char... You can't do both.

char is just a 1-byte integer. comparing a char with any other built-in numerical type is OK as far as C/C++ is concerned.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Atoi Function
1 Feb, 2008 - 04:58 AM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Bench @ 1 Feb, 2008 - 05:55 AM) *

QUOTE(no2pencil @ 1 Feb, 2008 - 03:27 AM) *

Your 1st if, is comparing to an int, the 2nd is comparing to a char... You can't do both.

char is just a 1-byte integer. comparing a char with any other built-in numerical type is OK as far as C/C++ is concerned.

So... what you are saying is... if I understand this correctly...

if(age[0] == 0) & if(age[0] == '0') are the same thing? Because I would think '0' is not the same as 0???

This post has been edited by no2pencil: 1 Feb, 2008 - 04:59 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Atoi Function
1 Feb, 2008 - 05:10 AM
Post #6

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(no2pencil @ 1 Feb, 2008 - 12:58 PM) *

QUOTE(Bench @ 1 Feb, 2008 - 05:55 AM) *

QUOTE(no2pencil @ 1 Feb, 2008 - 03:27 AM) *

Your 1st if, is comparing to an int, the 2nd is comparing to a char... You can't do both.

char is just a 1-byte integer. comparing a char with any other built-in numerical type is OK as far as C/C++ is concerned.

So... what you are saying is... if I understand this correctly...

if(age[0] == 0) & if(age[0] == '0') are the same thing? Because I would think '0' is not the same as 0???

No, they're not the same thing. One is a comparison to the integer value 0, one is a comparison to the character value '0' (Which will vary depending on the character set your system uses).

For if comparisons, they're still compatible types, however. it seems reasonable to want to compare to both (although, you might use '\0' instead of the int value 0 )
User is offlineProfile CardPM
+Quote Post

c--
RE: Atoi Function
1 Feb, 2008 - 07:48 AM
Post #7

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 27

I tried putting atoi after the user types the number, on the else function, but nothing worked...
Do you think it's my compiler? Can someone try that to see if it works for you??

When I type a letter, age is set to "0", but the compiler is ignoring my if function.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Atoi Function
1 Feb, 2008 - 08:10 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Please post your updated code.

http://www.cplusplus.com/reference/clibrar...tdlib/atoi.html

User is online!Profile CardPM
+Quote Post

barnwillyb
RE: Atoi Function
1 Feb, 2008 - 12:37 PM
Post #9

D.I.C Head
**

Joined: 22 May, 2007
Posts: 55


My Contributions
QUOTE(c-- @ 31 Jan, 2008 - 08:20 PM) *

Hello,

I try this code on my compiler, and it doesn't works. But I have tried that same dang code in other application, and it works fine. Does someone knows what the heck is going on with it?

Thanks in advance. smile.gif

This program asks a number, and if it's an invalid number will show a message. it converts a string to a integer.

CODE

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {


    char age[10];

    int intr = atoi ( age );

cout << "How old are you?" << endl;
cin >> age;

//If the user typed an invalid number or a negative number shows a message
if ( age[0] == 0 ){
    cout << "\a" << "You typed an invalid number" << endl << endl;}

if ( age[0] <= '0' )   {
    cout << "\a" << "You typed an invalid number" << endl << endl;}

else
{
    
    cout << "You have " << intr << " years old\n\n";
}


//Prevents the program to close after the user types his/her age
system("PAUSE");

return 0;
}



The problem is that my "if" function is not taking any action (if (age[0] == 0).


This will show you how to test the input:

CODE

//∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
//                         includes
//∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞

#include <iostream>
#include <cstdlib>        // for atoi

using namespace std;

//∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
//                       main listing
//∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞

int main () {
    char age[10];
    int intr;

    cout << "\n\tHow old are you: ";
        cin >> age;

    intr = atoi( age );
    // If the user typed a letter
    if ( isalpha( age[0] ))
        cout << "\tYou typed an invalid number!" << endl;

    // If the user typed a 0 or a negative number, shows a message
    else if (intr == 0 || intr < 0)
        cout << "\tYou typed an invalid number!" << endl;

    else
        cout << "\tYou are " << intr << " years old.\n";

    return 0;
}


User is offlineProfile CardPM
+Quote Post

Bench
RE: Atoi Function
1 Feb, 2008 - 02:03 PM
Post #10

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
the atoi and char[] constructs are very 'C'-like. char[] is in danger of overflowing, and atoi is an unreliable function, since it returns zero on an error. Which is very unhelpful if the string you're converting is a valid number which represents zero.

The preferred C++ way to convert from string to int uses stringstream objects instead
CODE
#include <string>
#include <sstream>
#include <iostream>

int main()
{
    using namespace std;
    string input;
    cout << "Enter your age: ";
    getline( cin, input );

    int age;
    stringstream ss(input);
    if( ss >> age )
        cout << "You are " << age << " years old" << endl;
    else
        cout << "Invalid input" << endl;
}

stringstream objects are basically memory buffers which hold string data. They're powerful, yet very easy to use (The syntax works the same as cin and cout). The if test checks to see whether the buffer is in a good state. The buffer will be set to a 'bad' state if its unable to convert from string to int (So you can tell whether the data stored by your int will be valid).
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 04:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month