Save a variable a first and last name that the user enters

1

I need to make a program in which the user enters their full name, their age, and print on the screen:

  

"Lord" name "your age is" age

I'm learning string of characters, but I'm not sure how to use them.

C ++ Code:

#include <iostream>
#include <conio.h>
#include <string.h>

using namespace std;

main() 
{
    int edad;
    char nombre[50];

    cout<<"ingrese nombre \n";
    cin>>nombre;

    gets(nombre);
    cout<<"ingrese edad \n";
    cin>>edad;

    if (edad>=0&&edad<=9){ cout<<nombre<<" su piso es 0 \n"; }
    if (edad>9&&edad<=19){ cout<<nombre<<" su piso es 1 \n"; }
    if (edad>19&&edad<=29){ cout<<nombre<<" su piso es 2 \n"; }
    if (edad>29&&edad<=39){ cout<<nombre<<" su piso es 3 \n"; }
    if (edad>39&&edad<=49){ cout<<nombre<<" su piso es 4 \n"; }
    if (edad>49&&edad<=59){ cout<<nombre<<" su piso es 5 \n"; }
    if (edad>59&&edad<=69){ cout<<nombre<<" su piso es 6 \n"; }
    if (edad>69&&edad<=79){ cout<<nombre<<" su piso es 7 \n"; }     
    getch();
}
    
asked by edwin_ucli 22.11.2016 в 17:12
source

1 answer

3

First of all the% main() in c ++ has to always return an integer if we want to follow the language standards. You can see what the documentation says about this here .

On the other hand you should not import libraries that you do not use so we eliminate string.h .

As for the problem itself, I suppose your headache comes because you want to capture a string with spaces, for example Pepito Pérez García . The capture with cin has the problem that when finding a space it stops so it will only capture the name but not the surnames.

There are functions to do this, gets() if I'm not mistaken it's marked deprecated in c++ and removed from c in the 2011 standard. We can use cin.getline() instead.

On the other hand, I recommend that you change your if by else if . The difference is that if all are if always evaluate them all, if you use a structure if, else if, else when a condition is evaluated as true the others are ignored, being more efficient.

The code would look something like this:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int edad;
    char nombre[50];

    cout<<"ingrese nombre \n";
    cin.getline(nombre,sizeof(nombre));

    cout<<"ingrese edad \n";
    cin>>edad;

    if (edad>=0&&edad<=9){ cout<<nombre<<" su piso es 0 \n"; }
    else if (edad>9&&edad<=19){ cout<<nombre<<" su piso es 1 \n"; }
    else if (edad>19&&edad<=29){ cout<<nombre<<" su piso es 2 \n"; }
    else if (edad>29&&edad<=39){ cout<<nombre<<" su piso es 3 \n"; }
    else if (edad>39&&edad<=49){ cout<<nombre<<" su piso es 4 \n"; }
    else if (edad>49&&edad<=59){ cout<<nombre<<" su piso es 5 \n"; }
    else if (edad>59&&edad<=69){ cout<<nombre<<" su piso es 6 \n"; }
    else if (edad>69&&edad<=79){ cout<<nombre<<" su piso es 7 \n"; }

   getch();
   return 0;
}

If you have freedom, it is better to use the library instead of an array of characters std::string that facilitates and makes more secure the work with chains in c++ . For this you can use std::getline() to capture the string:

#include <iostream>
#include <string>
#include <conio.h>


using namespace std;

int main()
{
    int edad;
    string nombre;

    cout<<"ingrese nombre \n";
    getline(cin,nombre);

    cout<<"ingrese edad \n";
    cin>>edad;

    if (edad>=0&&edad<=9){ cout<<nombre<<" su piso es 0 \n"; }
    else if (edad>9&&edad<=19){ cout<<nombre<<" su piso es 1 \n"; }
    else if (edad>19&&edad<=29){ cout<<nombre<<" su piso es 2 \n"; }
    else if (edad>29&&edad<=39){ cout<<nombre<<" su piso es 3 \n"; }
    else if (edad>39&&edad<=49){ cout<<nombre<<" su piso es 4 \n"; }
    else if (edad>49&&edad<=59){ cout<<nombre<<" su piso es 5 \n"; }
    else if (edad>59&&edad<=69){ cout<<nombre<<" su piso es 6 \n"; }
    else if (edad>69&&edad<=79){ cout<<nombre<<" su piso es 7 \n"; }

    getch();
    return 0;

}

I eliminate the use of conio.h by not being a standard library and having functions that do the same as getch(); in it, but this is not incorrect, they are only preferences.

    
answered by 22.11.2016 в 19:54