use getline without breaking

This is my code for a simple addition calculator (I'm learning how to use int function():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;

int calculate(int x, int y);

int main()
{
	; printf("Calculate: ");
	; string x_unconv = "0";
	; getline(cin, x_unconv);
	; int x = atoi(x_unconv.c_str());
	; printf(" + ");
	; string y_unconv = "0";
	; getline(cin, y_unconv);
	; int y = atoi(y_unconv.c_str());
	; printf(" = %d\n", calculate(x, y))
	; printf("Press any key to continue . . . ")
	; _getch()
	; return 0;
}

int calculate(int x, int y)
{
	; return x + y;
}


I expect it to come out like this when adding numbers:

Calculate: 

(type in 3 and press enter)

Calculate: 3 + 

(type in 5 and press enter)

Calculate: 3 + 5 = 8
Press any key to continue . . .


But instead it comes out like this:

Calculate: 3
 + 5
 = 8
Press any key to continue . . .


How do I use getline without having line breaks like this?
Generally speaking, you cannot intermix reading and writing in the console like this, because that's not how the console works (and it doesn't have anything to do with getline.) This looks like a poor use case for getline anyway.

What's with all the superfluous semi-colons and the C output functions??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int calculate(int x, int y);

int main()
{
    std::cout << "Enter two numbers:\n> ";

    int a, b;
    std::cin >> a >> b;

    std::cout << a << " + " << b << " = " << calculate(a,b) << '\n';
}

int calculate(int x, int y)
{
    return x + y;
}
thanks for the reply, i kind of understand why they wouldn't let you use getline without \n. After all, it does require you to press the enter key. I solved the problem by using Example 2 from here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682022(v=vs.85).aspx

"What's with all the superfluous semi-colons and the C output functions??"
Sorry, I have a really serious case of OCD and like to keep everything looking nice.

My new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <conio.h>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int sum(int x, int y);
void cls(HANDLE hConsole);

int main()
{
	; getx:
	; printf("Calculate: ");
	; string x_unconv = "";
	; getline(cin, x_unconv);
	; HANDLE hStdout;
	; hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
	; cls(hStdout);
	; if (x_unconv == "") {goto getx;};
	; int x = atoi(x_unconv.c_str());
	; gety:
	; printf("Calculate: %d + ", x);
	; string y_unconv = "0";
	; getline(cin, y_unconv);
	; hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
	; cls(hStdout);
	; if (y_unconv == "") { goto gety;};
	; int y = atoi(y_unconv.c_str());
	; printf("Calculate: %d + %d = %d\n", x, y, sum(x, y));
	; printf("Press any key to continue . . . ");
	; _getch();
	; return 0;
}

int sum(int x, int y)
{
	; return x + y;
}

void cls(HANDLE hConsole)
{
	; COORD coordScreen = { 0, 0 };
	; DWORD cCharsWritten;
	; CONSOLE_SCREEN_BUFFER_INFO csbi;
	; DWORD dwConSize;
	; if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; };
	; dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
	; if (!FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten)) { return; };
	; if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; };
	; if (!FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten)) { return; };
	; SetConsoleCursorPosition(hConsole, coordScreen);
}
Topic archived. No new replies allowed.