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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Infinity08Connect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
Infinity08Connect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.