How to print text after a input statment on the same line

How do I print text on the same line after an input statment?  What is the formatting

cout << " I worked ";
cin  >>hours;
cout <<" hours today " << endl;

this produces something like

I worked 46
hours

I'm looking for it to look like this

I worked 46 hours

where 46 is the cin statment

Thanks
WestonGroupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
That's not possible with standard C++. The problem is that most systems have a line based input ... In other words, the user has to press enter before input is available to the program. You'll have to make use of platform specific functionality, that allows to position the cursor anywhere.

What compiler are you using on what platform ?
0
WestonGroupAuthor Commented:
Dev C++
windows xp
0
Infinity08Commented:
Try this for example :
#include <iostream>
#include <windows.h>
 
using namespace std;
 
int main(void) {
  int hours = 0;
  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD pos = { 0 };
  CONSOLE_SCREEN_BUFFER_INFO sbinfo = { 0 };
  cout << " I worked ";
  GetConsoleScreenBufferInfo(hStdout, &sbinfo);
  pos = sbinfo.dwCursorPosition;
  cin >> hours;
  pos.X += 2;
  SetConsoleCursorPosition(hStdout, pos);
  cout << " hours today " << endl;
  return 0;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

WestonGroupAuthor Commented:
ok

I have another question not sure if I need to post a separate question...if so just le tme know

but I have 3 arrays

instead of the arrays printing in a straight line

how can I have them print side by side or in grid format?

employee name [7]
id[7]
rate[7]

so instead of

bob
123
15.00

joe
123
15.00

I want it to print

bob            joe
123            123
15.00         15.00


0
Infinity08Commented:
You can use the same technique as in your other question :

        http://www.experts-exchange.com/Programming/Languages/CPP/Q_23123110.html

to align the columns.

Then just iterate over the name array first, then over the id array, and then over the rate array.

Something like :

for (int i = 0; i < 7; ++i) {
    cout << left << setw(10) << name[i];
}
cout << endl;
for (int i = 0; i < 7; ++i) {
    cout << left << setw(10) << id[i];
}
cout << endl;
for (int i = 0; i < 7; ++i) {
    cout << left << setw(10) << rate[i];
}
cout << endl;

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
This isn't strictly speaking ANSI compliant but it will work on most compilers.
#include<cstdlib>
#include<conio.h>
#include<iostream>
using namespace std;
 
int main()
{
	cout << "I worked ";
	char c = 0;
	std::string hours; // Capture hours if you need it later
	while((c = getch()) != '\r'){ std::cout << c; hours += c;}
	cout << " hours today " << endl;
 
	system("pause");
 
	return 0;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.