[C++] Change system("PAUSE"); ?

Newbie Spellweaver
Joined
May 8, 2007
Messages
63
Reaction score
0
Im writing a advanced calculator for a school project.

calculator.cpp (main file)
Code:
#include <iostream>
#include <string>
#include "functions.cpp"
using namespace std;

int main()
{
    string choice;
    
    system("CLS");
    system("TITLE Advanced Calculator :: Version 1.0");
    system("COLOR 3");
    
    cout << "[CLIENT] Program started" << endl;
    cout << "\n";
    cout << "[CLIENT] The requests are: " << endl;
    cout << "[CLIENT] #01; 'addition' - Add two numbers." << endl;
    cout << "[CLIENT] #02; 'subtraction' - Subtract two numbers." << endl;
    cout << "[CLIENT] #03; 'multiply' - Multiply two numbers." << endl;
    cout << "[CLIENT] #04; 'division' - Divide two numbers." << endl;
    cout << "[CLIENT] #XX; 'exit' - Terminate the program." << endl;
    cout << "\n";
    cout << "[CLIENT] Enter a request:" << endl;
    cout << "\n";
    cout << "[REQUEST] ";
    cin >> choice;
    cout << "\n";
    
    if(choice == "addition" || choice == "Addition" || choice == "ADDITION")
    {
              fcAddition();
    }
    if(choice == "subtraction" || choice == "Subtraction" || choice == "SUBTRACTION")
    {
              fcSubtraction();
    }
    if(choice == "multiply" || choice == "Multiply" || choice == "MULTIPLY")
    {
              fcMultiply();
    }
    if(choice == "division" || choice == "Division" || choice == "DIVISION")
    {
              fcDivision();
    }
    if(choice == "exit" || choice == "Exit" || choice == "EXIT")
    {
              fcExit();
    }
    
    return 0;
}

functions.cpp (all functions)
Code:
#include <iostream>
using namespace std;

void fcAddition(void);
void fcSubtraction(void);
void fcMultiply(void);
void fcDivision(void);
void fcExit(void);

void fcAddition()
{
     int add_num1;
     int add_num2;
     
     cout << "[CLIENT] Enter the first number: ";
     cin >> add_num1;
     cout << "[CLIENT] Enter the second number: ";
     cin >> add_num2;
     cout << "\n";
     cout << "[CLIENT] The answer is = " << add_num1+add_num2 << endl;
     system("PAUSE");
}

void fcSubtraction()
{
     int sub_num1;
     int sub_num2;
     
     cout << "[CLIENT] Enter the first number: ";
     cin >> sub_num1;
     cout << "[CLIENT] Enter the second number: ";
     cin >> sub_num2;
     cout << "\n";
     cout << "[CLIENT] The answer is = " << sub_num1-sub_num2 << endl;
     system("PAUSE");
}

void fcMultiply()
{
     int mul_num1;
     int mul_num2;
     
     cout << "[CLIENT] Enter the first number: ";
     cin >> mul_num1;
     cout << "[CLIENT] Enter the second number: ";
     cin >> mul_num2;
     cout << "\n";
     cout << "[CLIENT] The answer is = " << mul_num1*mul_num2 << endl;
     system("PAUSE");
}

void fcDivision()
{
     int div_num1;
     int div_num2;
     
     cout << "[CLIENT] Enter the first number: ";
     cin >> div_num1;
     cout << "[CLIENT] Enter the second number: ";
     cin >> div_num2;
     cout << "\n";
     cout << "[CLIENT] The answer is = " << div_num1/div_num2 << endl;
     system("PAUSE");
}

void fcExit()
{
     system("EXIT");
}

Removed all the advanced functions so it's more readable.

Quick explanation:
As you see it has a function for each task.
When the user enters in the string eg "addition", the function addition will be called.
But in the functions itself, at the end when the answer is printed, i have to use system("PAUSE"); so the user can see the answer. But when im using system("PAUSE"); it terminates the program, and I just want it to go back to int main().
So I dont know if it's possible to change the function system("PAUSE"); just for my use?

And no, I can't use cin.get(); as I'm already using cin, and I dont want the user to write something, and then enter, just only when the user press any key.

Tried alot of things now. still didn't work.
Any ideas?
 
Last edited:
system("PAUSE"); does nothing else then waiting for user input to continue if I remember correctly.

However, this is not needed. You can put:

char *chr;
cin << chr;

And it will wait for user input. OR you could use the kbhit() function, its in windows.h if I remember correctly, but you can google it.

Also: it might be handy to make a batch file, and run the batch file which will run the program. This will give you an console that doesn't disappear after the program is finished:

run.bat:
Code:
yourprogramfilename
PAUSE

Also, I dont give out source code. Sorry.

If you really want your problem to be fixed, you need to give us the source. And believe me, we won't use it ^^ (lol)
 
system("PAUSE"); does nothing else then waiting for user input to continue if I remember correctly.

However, this is not needed. You can put:

char *chr;
cin << chr;

And it will wait for user input. OR you could use the kbhit() function, its in windows.h if I remember correctly, but you can google it.

Also: it might be handy to make a batch file, and run the batch file which will run the program. This will give you an console that doesn't disappear after the program is finished:

run.bat:
Code:
yourprogramfilename
PAUSE

If you really want your problem to be fixed, you need to give us the source. And believe me, we won't use it ^^ (lol)

Forgot to add something.
The functions is in another .cpp file.
I just include the cpp file.

But eh, I dont really understand that part of
char *chr;
cin << chr;

You aren't allowed to start a char array with a symbol, or is it just me?

Ill try the three things you suggested, thanks.

Btw, the source code is probably 400+ lines, so yeah, you migth not want to read it all aswell :P

EDIT:
char *chr;
cin << chr;

I tried that, i've put char *chr; where i have the other variables also, don't know if I was wrong about that part but it says this (I'm using dev-c++):
functions.cpp: In function fcAddition();
no match for
 
string str;
cin >> str;

Do you know about loops? Your main should be one simple do-while.

You need a header file for the functions.cpp where you declare all used functions. (You can't just #include a .cpp.)

Uh, if it's a school project aren't they supposed to teach this stuff before giving assignments? :huh:
Yeah I know about do-while but I don't know about all that increment thing.

Wow, this program is getting really messy now.
But in the header file, all that I need to do is put them in a class?
Im not quite sure how you make a class :P

I have always included the cpp file, and it works great.

And no, we aren't supposed to learn this. It's free subject.
Also, they wouldn't teach us at this grade :P

Source code in first post.
 
Well you have like
main()
{
<chunk of code>
return 0;
}

Just add two lines:
do
{
<chunk of code>
} while (true);

Read a book I tell you, because this is too basic to be asking.. what I mean is after someone solves this for you, you'll be back with another problem to be solved in no time if you don't learn this stuff by yourself.
 
Well you have like
main()
{
<chunk of code>
return 0;
}

Just add two lines:
do
{
<chunk of code>
} while (true);

Read a book I tell you, because this is too basic to be asking.. what I mean is after someone solves this for you, you'll be back with another problem to be solved in no time if you don't learn this stuff by yourself.
Still it won't go to int main()

But the main question of this thread is, if there is a way to change system("PAUSE") so it goes to int main() when a user press a key, like change it in it's library?

Yeah, I'm reading a book currently, but when I come to something I don't understand, I'm really stuck.
I don't go any further because it might be necessary later on. And that's the case rigth now :tongue:
 
I really don't see why you can't get something like this to work:

Code:
#include <iostream>

using namespace std;

int function()
{
    printf("We're in the function now, but we need a break!\n");
    system("PAUSE");
    printf("Lets continue...\n");
    return 5;
}

int main()
{
    printf("Output BEFORE pause\n");
    system("PAUSE");
    printf("Output AFTER pause\n");
    printf("Function output: %i\n", function());
    for (int i = 0; i < 5; i++)
    {
        system("PAUSE");
    }
    return 0;
}

Output:

Output BEFORE pause
Druk op een toets om door te gaan. . .
Output AFTER pause
We're in the function now, but we need a break!
Druk op een toets om door te gaan. . .
Lets continue...
Function output: 5
Druk op een toets om door te gaan. . .
Druk op een toets om door te gaan. . .
Druk op een toets om door te gaan. . .
Druk op een toets om door te gaan. . .
Druk op een toets om door te gaan. . .
 
After a function call is finished, it will return from the function to after where it was called. You don't need any gotos or anything.

About the book thing, it sounds like you've already skipped quite a few chapters.
Thank you, I made it work now.
Code:
#include <iostream>
#include <string>
#include "functions.h"
using namespace std;

int main()
{
    do
    { 
     string choice;
      
     system("CLS");
     system("TITLE Advanced Calculator :: Version 1.0");
     system("COLOR 3");
     
     cout << "[CLIENT] Program started" << endl;
     cout << "\n";
     cout << "[CLIENT] The requests are: " << endl;
     cout << "[CLIENT] #01; 'addition' - Add two numbers." << endl;
     cout << "[CLIENT] #02; 'subtraction' - Subtract two numbers." << endl;
     cout << "[CLIENT] #03; 'multiply' - Multiply two numbers." << endl;
     cout << "[CLIENT] #04; 'division' - Divide two numbers." << endl;
     cout << "[CLIENT] #XX; 'exit' - Terminate the program." << endl;
     cout << "\n";
     cout << "[CLIENT] Enter a request:" << endl;
     cout << "\n";
     cout << "[REQUEST] ";
     cin >> choice;
     cout << "\n";
    
     if(choice == "addition" || choice == "Addition" || choice == "ADDITION")
     {
               fcAddition();
     }
     if(choice == "subtraction" || choice == "Subtraction" || choice == "SUBTRACTION")
     {
               fcSubtraction();
     }
     if(choice == "multiply" || choice == "Multiply" || choice == "MULTIPLY")
     {
               fcMultiply();
     }
     if(choice == "division" || choice == "Division" || choice == "DIVISION")
     {
               fcDivision();
     }
     if(choice == "exit" || choice == "Exit" || choice == "EXIT")
     {
               fcExit();
     }
     
     system("CLS");
     system("TITLE Advanced Calculator :: Version 1.0");
     system("COLOR 3");
     
     cout << "[CLIENT] Program started" << endl;
     cout << "\n";
     cout << "[CLIENT] The requests are: " << endl;
     cout << "[CLIENT] #01; 'addition' - Add two numbers." << endl;
     cout << "[CLIENT] #02; 'subtraction' - Subtract two numbers." << endl;
     cout << "[CLIENT] #03; 'multiply' - Multiply two numbers." << endl;
     cout << "[CLIENT] #04; 'division' - Divide two numbers." << endl;
     cout << "[CLIENT] #XX; 'exit' - Terminate the program." << endl;
     cout << "\n";
     cout << "[CLIENT] Enter a request:" << endl;
     cout << "\n";
     cout << "[REQUEST] ";
     cin >> choice;
     cout << "\n";
     
     if(choice == "addition" || choice == "Addition" || choice == "ADDITION")
     {
               fcAddition();
     }
     if(choice == "subtraction" || choice == "Subtraction" || choice == "SUBTRACTION")
     {
               fcSubtraction();
     } 
     if(choice == "multiply" || choice == "Multiply" || choice == "MULTIPLY")
     {
               fcMultiply();
     }
     if(choice == "division" || choice == "Division" || choice == "DIVISION")
     {
               fcDivision();
     }
     if(choice == "exit" || choice == "Exit" || choice == "EXIT")
     {
     fcExit();
     }
    } while (true);
    
    return 0;
}

But is it really necessary to write it two times?

Oh, and at the book, I've read all the chapters (well not all in the whole book, but until for loop or something as I've stated). If there's anything I don't understand I read the whole **** again.
 
2 times? That would be pointless. The while loop already loops through it, now it loops through 2 same parts...useless.
 
No reason to have the code twice.

Also, no reason to have a do-while loop. Just make it into a while loop, eg:


Code:
int main() {

    while( true ) {
        // code
        // goes
        // here
    }

    return 0;
}

Another option would be something like:

Code:
int main() {

    string choice;
    do {
        // code
        // goes
        // here
    } while( choice != "exit" && choice != "Exit" && choice != "EXIT" );

    return 0;
}

In this case, "choice" would need to be outside the loop since it's not within the while's scope if it's left inside. You can do it as a plain while loop also, although in this case you might need to/want to initialize "choice" to some value.

Also, there is no need to use system("exit") since C++ has it's own exit() function. Of course, you can also simply break out of the loop by using the keyword "break", or do "return 0" from within the loop.

There are lots of options.
 
Alrigth, it works now with one code only.

Code:
#include <iostream>
#include <string>
#include "functions.h"
using namespace std;

int main()
{
    while(true)
    { 
     string choice;
      
     system("CLS");
     system("TITLE Advanced Calculator :: Version 1.0");
     system("COLOR 3");
     
     cout << "[CLIENT] Program started" << endl;
     cout << "\n";
     cout << "[CLIENT] The requests are: " << endl;
     cout << "[CLIENT] #01; 'addition' - Add two numbers." << endl;
     cout << "[CLIENT] #02; 'subtraction' - Subtract two numbers." << endl;
     cout << "[CLIENT] #03; 'multiply' - Multiply two numbers." << endl;
     cout << "[CLIENT] #04; 'division' - Divide two numbers." << endl;
     cout << "[CLIENT] #XX; 'exit' - Terminate the program." << endl;
     cout << "\n";
     cout << "[CLIENT] Enter a request:" << endl;
     cout << "\n";
     cout << "[REQUEST] ";
     cin >> choice;
     cout << "\n";
    
     if(choice == "addition" || choice == "Addition" || choice == "ADDITION")
     {
               fcAddition();
     }
     if(choice == "subtraction" || choice == "Subtraction" || choice == "SUBTRACTION")
     {
               fcSubtraction();
     }
     if(choice == "multiply" || choice == "Multiply" || choice == "MULTIPLY")
     {
               fcMultiply();
     }
     if(choice == "division" || choice == "Division" || choice == "DIVISION")
     {
               fcDivision();
     }
     if(choice == "exit" || choice == "Exit" || choice == "EXIT")
     {
               exit(1);
     }
    }
    
    return 0;
}

Thanks alot guys for your help, I really appreciate it :]
- If you have any more constructive critism, please post it.

Maybe those if statements could be simplyfied a little bit?
No clue how I would do it.
 
endl = endline = "\r\n" on a windows machine, "\n" on linux and "\r" on mac.

So instead of using \n you could use endl instead:

Code:
     cout << "[CLIENT] Enter a request:" << endl << endl << "[REQUEST] ";
     cin >> choice;
     cout << endl;

Also, you could use a switch for this:
Code:
     switch (choice)
     {
         case "addition":
         case "Addition":
         case "ADDITION":
         {
               fcAddition();
               break;
         }
    }
 
endl = endline = "\r\n" on a windows machine, "\n" on linux and "\r" on mac.

So instead of using \n you could use endl instead:



Also, you could use a switch for this:
Oh yeah, thanks for that endl. Coded it so fast that time that I didn't even think about that :tongue:

Tried using switch many times, got lots of errors.
Oh well, I'm gonna try again.

EDIT: Replaced all use of \n with endl;
EDIT2: Tried to use switch, but got the same errors as I got first time I tried to use it.

Errors:
calculator.cpp In function `int main()':
calculator.cpp switch quantity not an integer

Source:
Code:
#include <iostream>
#include <string>
#include "functions.h"
using namespace std;

int main()
{
    while(true)
    { 
     string choice;
      
     system("CLS");
     system("TITLE Advanced Calculator :: Version 1.0");
     system("COLOR 3");
     
     cout << "[CLIENT] Program started" << endl;
     cout << endl;
     cout << "[CLIENT] The requests are: " << endl;
     cout << "[CLIENT] #01; 'addition' - Add two numbers." << endl;
     cout << "[CLIENT] #02; 'subtraction' - Subtract two numbers." << endl;
     cout << "[CLIENT] #03; 'multiply' - Multiply two numbers." << endl;
     cout << "[CLIENT] #04; 'division' - Divide two numbers." << endl;
     cout << "[CLIENT] #XX; 'exit' - Terminate the program." << endl;
     cout << endl;
     cout << "[CLIENT] Enter a request:" << endl;
     cout << endl;
     cout << "[REQUEST] ";
     cin >> choice;
     cout << endl;
     
     switch(choice)
     {
                 case "addition":
                 case "Addition":
                 case "ADDITION":
                 {
                      fcAddition();
                      break;
                 }
     }
    }
    
    return 0;
}
 
Ah perhaps it has to be:

switch(choice.c_str())

And eventually with (char *) infront.
 
Back