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.

Dani AI

Generated

Short answer: you cannot force the console to accept user keystrokes at fixed column positions simply by printing a header with setw. setw and the other iomanip manipulators control how the program formats output (and limit extraction), they do not move the terminal cursor where the user types. See std::setw documentation.

A practical, portable solution is to print the header, read the values, then echo them back formatted so they line up under the header. This keeps the UI simple and works with any terminal. The following example shows that pattern (uses <iomanip> and fixed precision):

#include <iostream>
#include <iomanip>

int main() {
    double preScore, midScore, semiScore, finalScore;
    std::cout << std::left
              << std::setw(12) << "Prelim:"
              << std::setw(12) << "Midterm:"
              << std::setw(12) << "Semifinal:"
              << std::setw(12) << "Final:"
              << "Final grade:\n";

    std::cin >> preScore >> midScore >> semiScore >> finalScore;

    double avg = (preScore + midScore + semiScore + finalScore) / 4.0;

    std::cout << std::fixed << std::setprecision(1) << std::left
              << std::setw(12) << preScore
              << std::setw(12) << midScore
              << std::setw(12) << semiScore
              << std::setw(12) << finalScore
              << avg << '\n';
}

If the goal really is interactive, in-place editing under the header (so the user types into fixed fields), that is platform-specific and more work: use a console library (ncurses on Unix-like systems), ANSI escape sequences (many terminals), or Windows API SetConsoleCursorPosition. See ANSI escape codes and Windows Console API docs for details. For a simpler UI, prompting each grade on its own line (as suggested) or echoing formatted results (above) are the easiest and most portable fixes. Also note 's point: use setprecision/fixed to control decimal display when printing the echoed values.

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. :(

i'm lost.

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 Member #114542

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.