Home > Software engineering >  Trouble Inputting two short int's C
Trouble Inputting two short int's C

Time:09-25

Declare two short integer type variables (var7 and var8) and take their values using only one >>, that is, do this input in the same statement. Display the result

I believe I have the code all set up, just not sure if it's how I am inputting the info (5 "spacebar" 5) or how the code is laid out. However, the value for int 8 is always some crazy number, like 28900.

printf("enter an integer for var7 and var8 \n");
short int var7, var8;
cin >> var7, var8;
printf("Short integer value 7 is %d \n", var7);
printf("Short integer value 8 is %d \n", var8);

CodePudding user response:

For your own benefit, research the comma operator: "C comma operator".

In the mean time, this is how you input two variables:

std::cin >> variable_1 >> variable_2;

You may want to space separate them when entering on the console.

Edit 1: The program.
FYI, here's your program using C streams:

std::cout << "enter an integer for var7 and var8 \n";
short int var7;
short int var8;
std::cin >> var7 >> var8;   // Look ma, no comma!                              
std::cout << "Short integer value 7 is " << var7 << "\n";
std::cout << "Short integer value 8 is " << var8 << "\n";

CodePudding user response:

Well, some people love fun, so have fun:

int main()
{
    printf("enter an integer for var7 and var8 \n");
    short int var7, var8;

    short int* ptrs[] = { &var7, &var8 };

    for (int i = 0; i < sizeof(ptrs)/ sizeof(ptrs[0]);   i)
    {
        std::cin >> (*ptrs[i]);
    }

    printf("Short integer value 7 is %d \n", var7);
    printf("Short integer value 8 is %d \n", var8);
}

Of course, this is just fun-purpose code, but it fits the requirements.

CodePudding user response:

if you want to take multiple inputs in cin then use variable separated by >> operator

change

 cin >> var7, var8;

to

cin >> var7>> var8;
  •  Tags:  
  • c
  • Related