Suppose that num is an int variable and discard is a char variable. Assume the f
ID: 3821749 • Letter: S
Question
Suppose that num is an int variable and discard is a char variable.
Assume the following input data:
#34
What value (if any) is assigned to num and discard after each of the following statements executes? (Use the same input for each statement.)
a. cin.get (discard);
cin >> num;
b. discard = cin.peek();
cin >> num;
c. cin.get (discard);
cin.putback (discard);
cin >> discard;
cin >> num;
7. Suppose that name is a variable of type string. Write the input statement to read and store the input Melinda Clinton in name. (Assume that the input is from the standard input device.)
Explanation / Answer
a)
#include <iostream>
using namespace std;
int main()
{
int num;
char discard;
cout << "Enter values: ";
cin.get (discard);
cin >> num;
cout << "You entered " << endl;
cout << "num : "<< num << endl;
cout << "discard : " << discard << endl;
return 0;
}
output :
Enter values: #34
You entered
num : 34
discard : #
b)
#include <iostream>
using namespace std;
int main()
{
int num;
char discard;
cout << "Enter values: ";
discard = cin.peek();
cin >> num;
cout << "You entered " << endl;
cout << "num : "<< num << endl;
cout << "discard : " << discard << endl;
return 0;
}
output :
Enter values: #34
You entered
num : 0
discard : #
c)
#include <iostream>
using namespace std;
int main()
{
int num;
char discard;
cout << "Enter values: ";
cin.get (discard);
cin.putback (discard);
cin >> discard;
cin >> num;
cout << "You entered " << endl;
cout << "num : "<< num << endl;
cout << "discard : " << discard << endl;
return 0;
}
output:
Enter values: #34
You entered
num : 34
discard : #
7)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name ";
getline(cin,name);
cout << "You entered " << endl;
cout << "name : "<< name << endl;
return 0;
}
output :
Enter name Melinda Clinton
You entered
name : Melinda Clinton
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.