Exercises for Beginners 1

Hello, everyone.
I am here for sharing exercises with posible solutions with them.

If you have any other solutions for my exercises or you have new exercises share them on this topic. I will start to share in 1 hour. Thanks
Last edited on
Ask the user for two users' ages, and indicate who is older;

1. Solution:

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include using namespace std; int main() { int firstage; // an integer for storing first user's age int secondage; // an integer for storing second user's age string firstname; // a string for storing first user's name string secondname; // a string for storing second user's name cout<<"Input first user's name"<>firstage; // and store the age as first.age cout<<"Input "<>secondage; // and store the age as second.age // compare first user's age and second user's age if (firstage

2.Solution with structures (Thanks for Vlad From Moscow):

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main() { struct Person { std::string name; size_t age; } first, second; cout << "Input first user's name: "; getline( cin, first.name ); cout << "Input second user's name: "; getline( cin, second.name ); cout << "Input " << first.name << "'s age: "; cin >> first.age; cout << "Input " << second.name << "'s age: "; cin >> second.age; if ( first.age < second.age ) { cout << second.name << " older than " << first.name; } else if ( first.age == second.age ) { cout << first.name << "'s and " << second.name <<"'s age are same."; } else { cout << first.name << " older than " << second.name; } cout << endl; system( "pause" ); }``````
Last edited on
As an age can not be negative I think it would be better to declare x and y as unsigned int.
Last edited on
Also take into account that blank lines in the code are very important for its formatting and reading.
For example instead of

 ``123456`` `````` int x; // an integer for storing first user's age int y; // an integer for storing second user's age string first; // a string for storing first user's name string second; // a string for storing second user's name cout<<"Input first user's name"<

it would be much better to write at least

 ``1234567`` `````` int x; // an integer for storing first user's age int y; // an integer for storing second user's age string first; // a string for storing first user's name string second; // a string for storing second user's name cout<<"Input first user's name"<

Also never use this style pf placing braces

int main() {

It is simply a very bad style.
And instead of variables x and y it would be better to name them something derived from word age.:)
thanks for comments but i think no one will input a negative age so it is not a problem.

and for the second comment i will correct them
I would declare a structure something as

 ``1234567891011121314`` ``````int main() { struct Person { std::string name; size_t age; } first, second; // some stuff if ( first.age < second. age ) { cout << second.name << " older than " << first.name; } ``````

It is more readable than simple x, y first, second.:)
Last edited on
I still dont know structure and i am still a beginner so i am sharing the codes which i can understand. But if you share all the code using structure i can it as second solution
Or maybe even the following way

 ``12345678910111213141516`` ``````int main() { struct Person { std::string name; size_t age; bool operator <( const Person &p ) const { return ( age < p.age ); } bool operator ==( const Person &p ) const { return ( age == p.age ); } } first, second; // some stuff if ( first < second ) { cout << second.name << " older than " << first.name; } ``````
Deleted...
Last edited on
Without testing. So the code can contain a typo.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main() { struct Person { std::string name; size_t age; } first, second; cout << "Input first user's name: "; getline( cin, first.name ); cout << "Input second user's name: "; getline( cin, second.name ); cout << "Input " << first.name << "'s age: "; cin >> first.age; cout << "Input " << second.name << "'s age: "; cin >> second.age; if ( first.age < second.age ) { cout << second.name << " older than " << first.name; } else if ( first.age == second.age ) { cout << first.name << "'s and " << second.name <<"'s age are same."; } else { cout << first.name << " older than " << second.name; } cout << endl; system( "pause" ); }``````

The program is selfdocumented so it needs no comments. Though you can place a general comment before main.
Last edited on
working fine
It would be better if one thread will correspond to one your exercise.
thanks for your comment i will do it
The first your solution will not be compiled. You may not declare such identifies as (with embedded point)

int first.age; // an integer for storing first user's age
int second.age; // an integer for storing second user's age

string first.name; // a string for storing first user's name
string second.name; // a string for storing second user's name
is the problem "." ?
@vlad:
 system( "pause" );

you use `system()`?
is it matter?
what i can i use instead of system()? cin.get()?
Topic archived. No new replies allowed.