229k views
0 votes
2.36 LAB: Warm up: Variables, input, and casting (1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. Enter integer: 99 Enter double: 3.77 Enter character: z Enter string: Howdy 99 3.770000 z Howdy (2) Extend to also output in reverse. (1 pt) Enter integer: 99 Enter double: 3.77 Enter character: z Enter string: Howdy 99 3.770000 z Howdy Howdy z 3.770000 99 (3) Extend to cast the double to an integer, and output that integer. (2 pts) Enter integer: 99 Enter double: 3.77 Enter character: z Enter string: Howdy 99 3.770000 z Howdy Howdy z 3.770000 99 3.770000 cast to an integer is 3

User Erik Rose
by
6.5k points

2 Answers

2 votes

Answer:

howdy

Step-by-step explanation:

User Justin Woodmancy
by
6.8k points
6 votes

Answer:

The entire program is:

#include <iostream>

using namespace std;

int main() {

int userInt;

double userDouble;

char userChar;

string userString;

cout<<"Enter integer:"<<endl;

cin>>userInt;

cout<<"Enter double:"<<endl;

cin>>userDouble;

cout<<"Enter character:"<<endl;

cin>>userChar;

cout<<"Enter string:"<<endl;

cin>>userString;

cout<<userInt<<" "<<userDouble<<" "<<userChar<<" "<<userString<<endl;

cout<<endl;

cout<<userInt<<" "<<userDouble<<" "<<userChar<<" "<<userString<<endl<<userString<<" "<<userChar<<" "<<userDouble<<" "<<userInt<<endl;

cout<<endl;

cout<<userInt<<" "<<userDouble<<" "<<userChar<<" "<<userString<<endl<<userString<<" "<<userChar<<" "<<userDouble<<" "<<userInt<<endl<<userDouble<<" cast to an integer is "<<(int)userDouble;

return 0; }

The program in C language:

#include <stdio.h>

int main() {

int userInt;

double userDouble;

char userChar;

char userString[50];

printf("Enter integer: \\");

scanf("%d", &userInt);

printf("Enter double: \\");

scanf("%lf", &userDouble);

printf("Enter character: \\");

scanf(" %c", &userChar);

printf("Enter string: \\");

scanf("%s", userString);

printf("%d %lf %c %s\\", userInt, userDouble, userChar, userString);

printf("\\");

printf("%d %lf %c %s\\%s %c %lf %d \\", userInt, userDouble, userChar, userString, userString, userChar, userDouble, userInt);

printf("\\");

printf("%d %lf %c %s\\%s %c %lf %d\\%lf cast to an integer is %d \\", userInt, userDouble, userChar, userString, userString, userChar, userDouble, userInt, userDouble, (int)userDouble); }

Step-by-step explanation:

Lets do the program step by step:

1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space:

Solution:

The program is:

#include <iostream> //to use input output functions

using namespace std; //to identify objects cin cout

int main() { //start of main method

//declare an integer, a double, a character and a string variable

int userInt; //int type variable to store integer

double userDouble; //double type variable to store double precision floating point number

char userChar; //char type variable to store character

string userString; //string type variable to store a string

cout<<"Enter integer:"<<endl; //prompts user to enter an integer

cin>>userInt; //reads the input integer and store it to userInt variable

cout<<"Enter double:"<<endl; //prompts user to enter a double type value

cin>>userDouble; //reads the input double value and store it to userDouble variable

cout<<"Enter character:"<<endl; //prompts user to enter a character

cin>>userChar; //reads the input character and store it to userChar variable

cout<<"Enter string:"<<endl; //prompts user to enter a string

cin>>userString; //reads the input string and store it to userString variable

cout<<userInt<<" "<<userDouble<<" "<<userChar<<" "<<userString<<endl; //output the values on a single line separated by space

So the output of the entire program is:

Enter integer: 99 Enter double: 3.77 Enter character: z Enter string: Howdy 99 3.77 z Howdy

(2) Extend to also output in reverse.

Now the above code remains the same but add this output (cout) statement at the end:

cout<<userString<<" "<<userChar<<" "<<userDouble<<" "<<userInt;

Now the output with the same values given as input is:

Enter integer: 99 Enter double: 3.77 Enter character: z Enter string: Howdy

99 3.77 z Howdy Howdy z 3.77 99

(3) Extend to cast the double to an integer, and output that integer.

The rest of the code remains the same but add the following output (cout) statement in the end:

cout<<userDouble<<" cast to an integer is "<<(int)userDouble;

Now the output with the same values given as input is:

Enter integer: 99 Enter double: 3.77 Enter character: z Enter string: Howdy 99 3.77 z Howdy Howdy z 3.77 99 3.77 cast to an integer is 3

2.36 LAB: Warm up: Variables, input, and casting (1) Prompt the user to input an integer-example-1
2.36 LAB: Warm up: Variables, input, and casting (1) Prompt the user to input an integer-example-2