C++ program to develop a detailed program using all basic logics .

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). 
Example 1: 
Enter one integer: 123 
You entered the number: 123
 Enter four integers: 12 34 56 78 
You entered the numbers: 12 34 56 78 
Enter one integer and one real number: 4711 3.14159265
The real is: 3.142 
The integer is: 4711
Enter one real and one integer number: 2.71828183 1392 
The real is: ______2.718 
The integer is: ____1392 
Enter a character: a 
You entered: a
Enter a word: calvin 
You entered: calvin 
Enter an integer and a word: 32 students 
You entered ’32’ and ’students’.
Enter an character and a word: Q garden 
You entered the string "garden" and the character ’Q’. 
Enter a word and a real number: three .14 
You entered "three" and "0.140". 
Enter a text-line: The quick brown fox jumps over the lazy dog. 
You entered: "The quick brown fox jumps over the lazy dog." 
Enter a second line of text: That was a pangram phrase. 
You entered: “That was a pangram phrase.” 
Enter three words: Testing One Two 
You entered: “Testing One Two” 

Example 2:

Enter one integer: 123.4 
You entered the number: 123 
Enter four integers: -12 100034 56 0 
You entered the numbers: -12 100034 56 0 
Enter one integer and one real number: 4711 3.14159265 messing up 
The real is: 3.142 
The integer is: 4711 
Enter one real and one integer number: 1396 2.71828183 
The real is: ___1396.000 
The integer is: _______2 
Enter a character: abcd 
You entered: a Enter a word: 124calvin 
You entered: 124calvin 
Enter an integer and a word: 24students here 
You entered ’24’ and ’students’. 
Enter a character and a word: Qgarden 34 
You entered the string "garden" and the character ’Q’. 
Enter a word and a real number: three.14 16.17.18 
You entered "three.14" and "16.170". 
Enter a text-line: AND 1T D03S N0T rea11y matter. 
You entered: “AND 1T D03S N0T rea11y matter.” 
Enter a second line of text: Testing One Two three. 
You entered: “Testing One Two three.” 
Enter three words: Testing One Two three. 
You entered: “Testing One Two”

Code:

#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