Exercises for Beginners 1

closed account (1v5E3TCk)
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
closed account (1v5E3TCk)
Ask the user for two users' ages, and indicate who is older;

1. Solution:

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
#include <iostream>
#include <string>

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"<<endl;  // ask for first user's name
    getline( cin, firstname );      // and store the name as first
 
   cout<<"Input second user's name."<<endl;  // ask for second user's name
    getline(cin, secondname);   // and store the name as second

    cout<<"Input "<<firstname<<"'s age"<<endl; // ask for first user's age
    cin>>firstage;   // and store the age as first.age

    cout<<"Input "<<secondname<<"'s age"<<endl; // ask for second user's age
    cin>>secondage;  // and store the age as second.age

// compare first user's age and second user's age

    if (firstage<secondage){cout<<secondname<<" older than "<<firstname;}  

    else if (firstage==secondage) {cout<<firstname<<"'s and "<<secondname<<"'s age are same.";}

    else {cout<<firstname<<" older than "<<secondname<<endl;}
    system("pause");
    
    }

    


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

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
#include <iostream>
#include <string>
#include <cstdlib>

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

1
2
3
4
5
6
    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"<<endl;  // ask for first user's name
    getline( cin, first );  


it would be much better to write at least

1
2
3
4
5
6
7
    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"<<endl;  // ask for first user's name
    getline( cin, first );  


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.:)
closed account (1v5E3TCk)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
closed account (1v5E3TCk)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
   }  
closed account (1v5E3TCk)
Deleted...
Last edited on
Without testing. So the code can contain a typo.

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
#include <iostream>
#include <string>
#include <cstdlib>

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
closed account (1v5E3TCk)
working fine
It would be better if one thread will correspond to one your exercise.
closed account (1v5E3TCk)
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
closed account (1v5E3TCk)
is the problem "." ?
@vlad:

system( "pause" );

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