Write and Input on the Same Line

I was just wandering if I it is possible to write and input on the same line? Here is the program I would like to use it on:

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
53
54
55
56
#include <iostream>
#include <string>
#include "math.h"
#include "menu.h"

int main()
{
    using namespace std;
    
    int FirstInput, SecondInput; 
    
    int Choice = menu(); //CREATES MENU

    switch (Choice)
    {
           case 1: cout << "Please enter two numbers to be added\n";
                   cin >> FirstInput;
                   cout << " + ";
                   cin >> SecondInput;
                   cout << " = " << add(FirstInput, SecondInput);
                   break;
           
           case 2: cout << "Please enter two numbers to be subtracted\n";
                   cin >> FirstInput;
                   cout << " - ";
                   cin >> SecondInput;
                   cout << " = " << sub(FirstInput, SecondInput);
                   break;
           
           case 3: cout << "Please enter two numbers to be multiplied\n";
                   cin >> FirstInput;
                   cout << " * ";
                   cin >> SecondInput;
                   cout << " = " << multi(FirstInput, SecondInput);
                   break;
                   
           case 4: cout << "Please enter two numbers to be divided\n";
                   cin >> FirstInput;
                   cout << " / ";
                   cin >> SecondInput;
                   cout << " = " << divide(FirstInput, SecondInput);
                   break;
                   
           case 5: cout << "Please enter two numbers to be modulized\n";
                   cin >> FirstInput;
                   cout << " % ";
                   cin >> SecondInput;
                   cout << " = " << mod(FirstInput, SecondInput);
                   break;
    }
    
    cout << "\n\nPlease press ENTER to exit\n";
    cin.get();
    cin.get();
    return 0;
}


The output would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
***************************************************
W E L C O M E   T O  T H E  M E N U  F O R  M A T H
(1) Add
(2) Subtract
(3) Multiply
(4) Divide
(5) Modulize
***************************************************

Please make a selection: 1

Please enter two numbers to be added
6
 + 3
 = 9

Please press ENTER to exit


Obviously it should look like this:

1
2
...Please enter two number to be added
6 + 3 = 9...


Thanks in advance.
cin reads input only after the user has entered 'enter' ( which causes a newline )
You can do that using the curses libraries ( ncurses or pdcurses )
Yes, it is possible, but it is probably not worth the grief. Besides just the technical considerations of doing it all on one line, there is also trying to determine when the user has finished typing a number. (Is '9' the number 9 or the first part of the number 92?)

In your particular case, I would suggest just letting the user type the whole equation, and switching based upon the operator used.

W E L C O M E   T O   T H E   M A T H   M A C H I N E
You can add, subtract, multiply, divide, and get the remainder of numbers the normal way.
For example, type "3 + 7" to get 10.
Please enter an equation, or nothing to quit.
> 6 + 3
9
> 13 % 4
1
>
Good bye.


If you are undeterred, however, you can use some simple terminal magic to position the cursor where you want.

Clear the screen: http://www.cplusplus.com/forum/articles/10515/#msg49080
Position the cursor.
1
2
3
4
5
6
7
8
// Windows
#include <windows.h>

bool GotoXY( unsigned x, unsigned y )
  {
  COORD position = { x, y };
  return SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), position );
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// POSIX (Unix, Linux, etc)
// See also http://www.cplusplus.com/forum/beginner/22687/page2.html#msg121058
//
#include <unistd.h>
#include <term.h>

bool GotoXY( unsigned x, unsigned y )
  {
  if (!cur_term)
    {
    int success;
    setupterm( NULL, STDOUT_FILENO, &success );
    if (success <= 0)
      return false;
    }
  return putp( tigetstr( "cup" ), y, x, 0, 0, 0, 0, 0, 0, 0 ) != ERR;
  }

Now once the user inputs his value, goto the previous line and write everything out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ClearScreen();
cout << "Please enter two numbers to be added.\n\n";
cin >> first;

GotoXY( 0, 1 );
cout << first << " + ";
cin >> second;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

GotoXY( 0, 1 );
cout << first << " + " << second << " = " << (first + second) << "\n\n";

cout << "Please press ENTER to continue";
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

This is a very simple solution. You will have to work some more to improve it against people entering things like "lolz this was spozed 2 B an integer".

Hope this helps.
I didn't realize it would be that complicated. I'll just make the user input information on separate lines like I normally would do. Thanks though, I'll try these later when I'm more experienced in C++.
Is it possible to check a variables data type? For instance, if x = a int type, do this.
That's kind of a silly question. How can you not know what type x is? You have to give it a type when you declare it.
I was thinking to use that so if someone was to enter a float into an int variable, the program wouldn't crash...cause I don't how to...which is why I am in the beginners forums. It's harder than Ruby...
The input streams do that for you. You have to be aware, though, that whenever you ask the user for something, he will always press the ENTER key when done.

For example:
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
int n;

cout << "Please enter an integer> ";
while (true)
  {
  // Try to read the integer
  cin >> n;

  // User failed to enter anything numeric
  if (!cin)
    {
    cin.clear();
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
    cout << "Hey, an INTEGER please> ";
    continue;
    }

  // Now, since the user had to press ENTER, lets get rid of it and make sure
  // that he didn't have any non-integer stuff in his input.
  string s;
  getline( cin, s );
  s.erase( 0, s.find_first_not_of( " \t" ) );  // trim any leading spaces or tabs
  if (!s.empty())
    {
    cout << "Hey! What's that extra stuff? Enter an INTEGER please> ";
    continue;
    }

  // If we've gotten this far, then the user entered a valid integer by itself.
  break;
  }

cout << "Good job! The integer you entered is '" << n << "'.\n";

Hope this helps.
Thanks, it helps a lot.
Topic archived. No new replies allowed.