C++ program to develop a detailed program using all basic logics .
Write a program that behaves as the program in example 1 on the next page. (In the examples text
entered by the user is shown in bold to distinguish it from the text produced by the program.) Test
your program carefully with all conceivable input. When and why does it fail? Modify your program
to handle more input. It should now work for both example 1 and example 2. You will need to read
up on and the input buffer. Pay careful attention to the example output and match the
output of your program to them exactly. Note specifically the right alignment of the reals and
integers. Demonstrate to your assistant when you think your program is correct. Follow the
instructions for how to demonstrate given earlier in the document.
You may assume the user never enter letters when the program expects numbers. In the final version
you must be prepared the user enters more digits and letters on each line than asked for (example 2).
Some students always point out that you cannot read four integers using only one integer variable.
But you can. And you shall. Go do your homework on the input buffer (cin,cout).
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{ string
str;
string
str2;
string
str3;
string
str4;
string
str5;
string
str6;
//one integer
int a;
cout<<"Enter one integer: ";
cin>>a;
cout<<"You entered the number: "<<a<<endl;
//Four integers
int b,c,d,e;
cout<<"Enter four integers: ";
cin>>a;
b=a;
cin>>a;
c=a;
cin>>a;
d=a;
cin>>a;
e=a;
cout<<"You entered the numbers: "<<b;
cout<<" "<<c;
cout<<" "<<d;
cout<<" "<<e<<endl;
//one integer one real
float f;
cout<<"Enter one integer and one real number: ";
cin>>a>>f;
cout<<"The real is: "<<setprecision(4)<<f<<endl;
cout<<"The integer is: "<<a<<endl;
//one real one integer
cout<<"Enter one real and one integer number: ";
cin>>f>>a;
cout<<"The real is: "<<setfill ('_') <<setw
(10)<<setprecision(4)<<fixed<<f<<endl;
cout<<"The integer is: "<<setfill ('_') <<setw (8)<<a<<endl;
//One charactor
char ch;
cout<<"Enter a character:";
cin>>ch;
cout<<"You entered: "<<ch<<endl;
//one word
string
string;
cout<<"Enter a word: ";
cin.ignore();
getline(cin,string);
cout<<"You entered: "<<string<<endl;
cin.ignore();
//an integer and a word:
int integer;
cout<<"Enter an integer and a word: ";
cin>>integer;
cin.ignore();
getline(cin,str);
cin.ignore();
cout<<"You entered \'"<<integer<<"\' and \'";
cout<<str<<"\'."<<endl;
//Enter an character and a word:
cout<<"Enter an character and a word:";
cin>>ch;
cin.ignore();
getline(cin,str2);
cout<<"You entered the string \""<<str2<<"\" and the character \'"<<ch<<"\'."<<endl;
//Enter a word and a real number:
float real;
cout<<"Enter a word and a real number: ";
cin.ignore();
getline(cin,str3);
cin.ignore();
cin>>real;
cout<<"You entered \""<<str3<<"\" and \"";
cout<<setprecision(4)<<real<<"\"."<<endl;
//Enter a text-line:
cout<<"Enter a text-line: ";
cin.ignore();
getline(cin,str4);
cout<<"You entered: \""<<str4<<"\""<<endl;
//Enter a second line of text:
cout<<"Enter a second line of text: ";
cin.ignore();
getline(cin,str5);
cout<<"You entered: \""<<str5<<"\""<<endl;
//Enter three words:
cout<<"Enter three words: ";
cin.ignore();
getline(cin,str6);
cout<<"You entered: \""<<str6<<"\""<<endl;
system("pause");
return 0;
}
Output:
Comments
Post a Comment