-
Problems with code
Code:
#include<iostream>
using namespace std;
int main()
{
int year_of_birth;
cout<<"Please enter the year you were born below, and It will tell you how much older or younger you are from me\n";
cin>> year_of_birth;
cin.ignore();
if ( year_of_birth < 1980 ) {
cout<<" You are older than me by"<<( year_of_birth - 1980 )<<"year(s)!\n";
}
else ( year_of_birth > 1980 ) {
cout<<" You are younger than me by"<<( 1980 / year_of_birth )"year(s)!\n";
}
cin.get();
}
I'm very much a begginer in C++ programming, and 2 days ago started following the tutorial. Did the examples and decided I was going to try making my own simple program. The Program is designed to figure out how many years apart your year of birth Is from my date of birth and output this data. The problem Is I don't know much yet about programming as I stated earlier.
I was wondering If anyone could tell me what is wronge with it.
Also I'm aware there are some major problems with it just by looking at them, I just don't know what they are.
Thank You in Advance
-
it doesnt need to be:
Code:
cout<<" You are older than me by"<<( year_of_birth - 1980 )<<"year(s)!\n";
because you dont have to put the variable in "(" and ")"... just make it:
Code:
cout<<" You are older than me by"<< year_of_birth - 1980 <<"year(s)!\n";
and i think that will make it work, im working on something right now so i cant check ATM... BTW im new to c++ myself (3rd or fourth day... forget which one...)
-
paranthesis dont matter, you could do this
cout << ((((((((((8-((((((((((3)))))))))))))))))))) << endl;
and it would work fine.
-
hhmmm... well, thats why i put that im new too :D
oh well... i tried
-
Code:
#include<iostream>
using namespace std;
int main()
{
int year_of_birth;
cout<<"Please enter the year you were born below, and It will tell you how much older or younger you are from me\n";
cin>> year_of_birth;
cin.ignore();
if ( year_of_birth < 1980 ) {
cout<<" You are older than me by "<<( 1980 - year_of_birth )<<"year(s)!\n"; //bad logic here, gives negative the other way
}
else { //dont need yearbirth > 1980 unless you were doing an ifelse statement
cout<<" You are younger than me by "<<( year_of_birth -1980 ) << "year(s)!\n"; //forgot an extra "<<" and bad logic again
}
cin.get();
return 0;
}
-
Code:
#include<iostream>
using namespace std;
int main()
{
int year_of_birth;
cout<<"Please enter the year you were born below, and It will tell you how much older or younger you are from me\n";
cin>> year_of_birth;
cin.ignore();
if ( year_of_birth < 1980 ) {
cout<<" You are older than me by"<<( 1980 - year_of_birth )<<"year(s)!\n";
}
else if( year_of_birth > 1980 ) {
cout<<" You are younger than me by"<<( year_of_birth - 1980 )<<"year(s)!\n";
}
else
cout<<" Same age!!!"<<endl;
cin.get();
}
-
It's still saying there is an error, Is there anything else I can do to make it work.
thanx for the help
-
nevermind the prev. post works
-
wow we all posted at the same time :) beat u to it
-
lol, by the way do any of you know how to keep the window open for more than one entry?
It will be greatly appreciated
-
if you want more than one, put it in a loop. you could use a while statement and break out of it if a sentinel value is entered or you could use a for loop and loop it as many times as you want
-
thank you all, you have been most helpful.