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.
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.
[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
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.
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
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'
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.
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.
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.
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.
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
elseif (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);
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;}
elseif (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.
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.