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: \n");
scanf("%d", &userInt);
printf("Enter double: \n");
scanf("%lf", &userDouble);
printf("Enter character: \n");
scanf(" %c", &userChar);
printf("Enter string: \n");
scanf("%s", userString);
printf("%d %lf %c %s\n", userInt, userDouble, userChar, userString);
printf("\n");
printf("%d %lf %c %s\n%s %c %lf %d \n", userInt, userDouble, userChar, userString, userString, userChar, userDouble, userInt);
printf("\n");
printf("%d %lf %c %s\n%s %c %lf %d\n%lf cast to an integer is %d \n", userInt, userDouble, userChar, userString, userString, userChar, userDouble, userInt, userDouble, (int)userDouble); }
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