Hello ma'am/sir
any help for the codes of input to be align.heres my code:

#include<iostream>
using namespace std;
int main()
{
float pre, mid, semi, final, total;
cout << "Prelim: "<<setw(20)<< "Midterm: "<<setw(20)<< "Semifinal: "<<setw(20)<< "Final: "<<setw(20)<< "Final grade: \n";
cin>>pre>>mid>>semi>>final>>total;
total = (pre+mid+semi+final)/4;
system("pause");
return 0;
}

expected execution:


Prelim: Midterm: Semifinal: Final: Final grade:
1.3 1.2 1.5 1.2 1.3


any help, or just hints. Thanks.

Recommended Answers

All 8 Replies

Ye' never state what ye' problems is, but i'll guess it has something to do with float precision. there are a couple o' ways to set precision:

cout << setprecision(2);

//  or 

cout.setf(ios::fixed);
cout.setf(ios::showbase);
cout.precision(2);

Sorry for that.
The problem is that i don't get the cin align to the cout above. i want it align. just don't know the padding syntax for cin. :(

check this out:-
1.3 space 1.2 space 1.5 space 1.2 space 1.3 enter

#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
float pre, mid, semi, final, total;
cout << "Prelim: "<<setw(5)<< "Midterm: "<<setw(5)<< "Semifinal: "<<setw(5)<< "Final: "<<setw(5)<< "Final grade: \n";
cin>>pre>>mid>>semi>>final;
total = (pre+mid+semi+final)/4;
cout<<"total is="<<total;
system("pause");
return 0;
}

ayeshashahid, can't be done, next input will put in the nextline.

Member Avatar for Mouche

You should just ask for each grade individually. Print the test such as "Prelim" and then wait for input and <enter>. This could be the resulting program output:

Prelim:
1.3
Midterm:
1.2
Semifinal:
1.5
Final:
1.2
Final grade:
1.3

ayeshashahid, can't be done, next input will put in the nextline.

Since there is only one cin , there is no next input.

You should just ask for each grade individually. Print the test such as "Prelim" and then wait for input and <enter>. This could be the resulting program output:

And how does that help him align the output to the header he displayed :icon_rolleyes:

float pre, mid, semi, final, total;
	cout <<setw(20)<<"Prelim: "<<setw(20)<< "Midterm: "<<setw(20)<< "Semifinal: "<<setw(20)<< "Final: "<<setw(20)<< "Final grade: \n";
	cin>>pre>>mid>>semi>>final>>total;
	total = (pre+mid+semi+final)/4;
	cout<<total;
	system("pause");
	return 0;
commented: What's this post about? No comments and it look just like the code the OP posted. Therefore, this is worthless! -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.