Trying to deal with string to int conversion

Hello I'm taking an intro c++ class and we are creating a program to use zeller's equation from a user inputted date. I have 2 problems with my code that I can't figure out if someone would be willing to help.

Edit: Thank you to everyone so far. My original problems have been solved.

1
2
3
#include <cstdlib>
#include <iostream>
using namespace std;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (exit==0)
        {
            //get month
            do
            {
                cout<<endl<<"Month?"<<endl;
                cin>>temp;
                if (is int(temp)) //here
                {
                     month=atoi(temp); //here
                    if (month<0);
                    {
                        exit=1;
                        check=1;
                        cout<<"Exiting program";
                    }//end inside if
                    if (month==0||month>12)
                        cout<<"Error:the months of the year range from 1-12";
                    else
                        check=1;}
                else
                    cout<<"please input an integer value";}
            while(check==0);
            check=0;



1.is int is not defined
2.atoi won't convert
The user inputs a negative value at any time to terminate the program.
Last edited on
[code] "Please use code tags" [/code]
http://en.wikipedia.org/wiki/Strong_typing
int n; creates a variable named n of integer type. It will be an integer till the moment it dies (goes out of scope)
So it doesn't make sense to ask (integer? n) like in scheme

Maybe you want isdigit(), but note that it receives only 1 character
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

I think that you could have name collision. exit() is a function defined in cstdlib
I'm inputting to a string in case the user inputs a non integer value. Then I want to check that the string can be converted to an integer and assign it in the correct place.

Since the input can be multiple digits using the char form and then going to isdigit won't work easily. I would need a lot more error checking that I have no clue how to do as of yet.

P.S. Thanks for teaching me how to make the code box.
Last edited on
bump:(
you can use a stringstream.

1
2
3
4
5
string input;
cin >> input;
stringstream ss(input);
int number;
ss >> number;



that will convert input to an integer. play around to see how it works with different inputs (positive/negativ numbers, text, mix of digits and chars)

or just read in an integer. if the input is not a proper integer it'll be 0, i think. but im not sure here, so play around again ;)
Conversion http://www.cplusplus.com/forum/articles/9645/
Try to convert, if it fails then the input was wrong.
If you want to avoid input like 12anx just check that the stream reached eof
I replaced my is int and atoi with
1
2
month=y(  temp);
                if (month!=10000)

and
1
2
3
4
5
int y(string x)
    {stringstream convert(x);
    if (!(convert>>y))
        y=10000;
return y;}

This wiped out all errors in the main but this function still has 4 errors.
1 variable `std::stringstream convert' has initializer but incomplete type
2 assignment of function `int y(std::string)'
3 cannot convert `int' to `int ()(std::string)' in assignment
4 invalid conversion from `int (*)(std::string)' to `int'
1
2
3
4
5
6
7
8
int y(string x)
{
      stringstream convert;
      convert >> x;
      int value;
      convert << value;
      ...
}

should be more what it looks like.
Last edited on
why would I need another int?
also now I'm getting ambiguous overload operator>> in ss>>y ,and a big note,
1
2
3
4
5
6
7
int y(string x)
    {stringstream ss(x);
    if (!(ss>>y))
        y=10000;
    else
        ss>>y
return y;}


with the original problems when I included <sstream>

also tried this
1
2
3
4
5
6
7
int y(string x)
    {stringstream convert;
    convert>>x;
    convert<<y;
    if (!(convert>>y))
        y=10000;
return y;}

and a warning was added
156 C:\Users\Dan the Man\Documents\Project1.3.cpp [Warning] the address of `int y(std::string)', will always evaluate as `true'

I also feel like those arrows in yours ,and now mine, are going the wrong way for string to int though I don't have much of a clue.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int y(string x)
{
       int result = 0;
       stringstream convert;  // creat stream
       convert >> x;   // put one element in stream
       convert << result;   // take one element out.
       if (!result) // if(!(convert>>y) will bring try to bring out a second element.
       {
             result=10000;
       }
       return result;
}


The fact that your code compiles is beyond me.. Y would always be ambigious.. It is a function that needs an integer returned and you are trying to use it as the variable all the time.
Last edited on
y is a function, not an integer. This is not Pascal. (you could use a better name too)
Read the arrows as data flow.
1
2
convert >> x; // Put a value in the variable x.
convert << x; // Get the value of x 

Look at Mathes's example.
I always though of >> as "send to".
so x is the string that I want to >> the converter.
then I want to pick up from the converter and send it to results.
I've changed it and got it to compile and start to work now.
1
2
3
4
5
6
7
8
9
10
11
int convert(string x)
{
       int result = 0;
            stringstream convert;
            convert << x;
            if (!(convert>>result)
                 result=10000
            else
                 convert >> result;
       return result;
}


Thanks everyone I got the converter to work but now I've come up with 2 more problems.

My 2 new problems are
1. The converter still allows non integer inputs such as 2001.2 or 2000y I would like those to also have result=10000 but also still allow - values.
2. My program goes through three input verification loops and each one is so far set to use this function. Right now it seems like when the program goes to use the function a second time it closes. I was wondering if it is possible to reuse a function or if I had some other problem to check for.
Last edited on
The checking of a string for characters other than numbers might suit your situations.

For Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

bool verify(string inString)
{
       bool ok = true;
       int length = inString.length();

       for(int counter = 0; counter < length, counter++)
       {
            if (!isdigit(inString[counter]))
            {                  
                  ok = false;
                  break;
             }
        }
        return ok;
}


This may not compile but I think you get the basic idea and can fix it.
Last edited on
In the conversion, check that you used all the characters.
1
2
if( not convert.eof() ) //there were remaining characters
  result = 1000;

1
2
3
4
            if (!(convert>>result)
                 result=10000
            else
                 convert >> result; //No, you already read it in the if condition 

In your code you have name collision (function convert and stream convert) scope

2. My program goes through three input verification loops and each one is so far set to use this function. Right now it seems like when the program goes to use the function a second time it closes.
I don't understand you. Please post those calls.
Last edited on
first two loops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 do
        {
            cout<<endl<<"Year of birth?"<<endl;
            cin>>temp;
            year=convert( temp);
            if (year!=10000)
                {
                    if (year<0)
                        {
                        check=1;
                        exit=1;
                        cout<<"Exiting program press any can + Enter.";
                        }//end inside if
                    else if (year<1582||year>4902)
                        cout<<endl<<"Error: Year is outside the given range (1582-4902)";
                    else
                        check=1;
                }//end outside if
            else
                cout<<"please input an integer value";
        }while (check==0);//end get year loop check
        check=0;
        if (exit==0)
        {
            //get month
            do
            {
                cout<<endl<<"Month?"<<endl;
                cin>>temp;
                month=convert( temp);
                if (month!=10000)
                {
                    if (month<0);
                    {
                        exit=1;
                        check=1;
                        cout<<"Exiting program press any key + Enter.";
                    }//end inside if
                    if (month==0||month>12)
                        cout<<"Error:the months of the year range from 1-12";
                    else
                        check=1;}
                else
                    cout<<"please input an integer value";}
            while(check==0);


convert function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int convert(string x)
{
       int length=x.length();
       int counter=0;    
       int result = 0;
       char ch;
       do
       {
            ch=x[counter];
            if (counter==0)
                {if ((ch<48||ch>57)&&ch!=45)
                result =10000;}
            else if (ch<48||ch>57)
                result=10000;
            counter++;
       }while (counter<length&&result!=10000);
       if (result!=10000)
            {stringstream convert;
            convert << x;
            if (!(convert>>result))//just in case something bypassed the earlier checks
                result=10000;
            else
                convert >> result;}
       return result;
}


program does this
year?
2000
month?
6
exiting program

The second time I run through the function it gives me a - value regardless of the input.
Last edited on
Semicolon in line 33 if (month<0);

Now about the convert function. !!? The idea was that the stringstream will do the parse for you.
You are trying to read the number twice. (in line 20 and 23).
Don't use magic numbers.

1
2
3
4
5
6
//converts s into n. Returns if it was "successful"
bool string_to_int( const std::string &s, int &n){
  std::istringstream ss(s);
  ss >> n;
  return ss.eof(); //all characters read 
}
Thank you very much everyone:)
It doesn't surprise me that such a frustrating mistake on my part came from ;.
This is only my second programming class and the first one was GUIs in visual basic.
Glad you got it to work.
Topic archived. No new replies allowed.