2

I'm trying to do a calculator type thing to practice with functions (I'm a beginner) and for the user to use, addition, subtraction, mult. or division the user has to choose a mode which is the job of the variable mode so I used cin so the user can enter a number. but once the user chooses the mode then the user would need to input the values but to do that i would need to use cin again, but the screen where the user inputs the value doesn't appear. what should I do? (this is not complete)

#include <iostream>
using namespace std;

double mode4 (double x, double y){
    double sum;
    sum = x + y;
    cout << "sum is: " << sum <<endl;
    return 0;
}

int main() {
   int *mode = new int;

cin >> *mode;
if (*mode > 4 || *mode == 0){
  *mode = 4; 
}

if (*mode == 4){
   double num1;
   double num2; 
   cin >> num1 >> num2;
    mode4(num1, num2);
    delete mode;
}

cout << *mode << endl;
    return 0;
}
  • Can you also specify what input you are giving for *mode? Also, a screenshot will help. – Deepak Patankar Jun 13 '20 at 05:53
  • Please provide a [MCVE]. – David Grayson Jun 13 '20 at 05:54
  • I tried running the code and its working at expected, If you input the mode as 0, the the input screen won't disappear as it has to take two more input num1 and num2, once you input this two values it will find the sum of the numbers and the input screen will disappear. – Deepak Patankar Jun 13 '20 at 05:56
  • Can you please explain, what behaviour you are expecting. For next cin you won't get a different screen, you will have to give the input in the same screen. – Deepak Patankar Jun 13 '20 at 05:57
  • Could you please specify what IDE you are using on which operating system, or if you are working from the command line? The answer by @DeepakPatankar shows exactly how your program would behave under the linux command line, but if you are using an IDE, the area where you enter input for your program may look different. – Diggs Jun 13 '20 at 06:28
  • I'm using Sololearn's IDE which is a website that teaches you about codig, as I read the comments I learned what I was doing did not make sense so I came up with another idea but I'm having some issues with it but now it can't be classified as the same question so I just decided to create another question. thanks for the help on this one. – Anthony Salazar Jun 14 '20 at 07:18

2 Answers2

3

You will have to input the value of num1 and num2 in the same screen, cin doesn't generate a new screen for every time it is called. This is how your calculator screen looks like :

0
2 3
sum is: 5
4

If you want to take the next input in a new screen then you can use this answer

Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35
  • 1
    Good effort on the answer, but please do not post pictures of text, instead post the text itself indented by 4-spaces so it formats as fixed width. Why? What would require ~ 70 bytes to store as text, requires 53,587 bytes as your image -- it adds up... – David C. Rankin Jun 13 '20 at 06:14
  • Hey @DavidC.Rankin, Your point is valid I have updated my answer, thanks for the feedback! – Deepak Patankar Jun 13 '20 at 06:34
1

Not the answer to your question but I just have to mention

int *mode = new int;

cin >> *mode;
if (*mode > 4 || *mode == 0){
    *mode = 4; 
}

Don't gratuitously use pointers, this is simpler, safer, more efficient, less typing etc.

int mode;

cin >> mode;
if (mode > 4 || mode == 0){
    mode = 4; 
}

etc.

If you need an integer variable just declare one. There's no need to use an integer pointer and allocate the integer.

john
  • 85,011
  • 4
  • 57
  • 81
  • You are right, but put that like a comment, not like an answer. Because it's not an answer and will be deleted. – TPAKTOPA Jun 13 '20 at 13:21