# C++ question



## jacy (Jul 19, 2004)

Hello,
Here is my program where am trying to calculate the salary. It compiles, but when i run it, it doesn't display the salary. Am using dev c++, thanks.

#include <iostream>
using namespace std;
int main ()
{
float sales;
float salary;
cout << "Enter the sales $ ";
cin >> sales;
salary = sales * 0.09 + 200;
cout << "Salary is "<< salary;

cin.get();
return 0;
}


----------



## hytechpro (Sep 17, 2005)

you said it compiles but on running shows nothig

is code compile sussfully?
or showing any warnings or error...


----------



## jacy (Jul 19, 2004)

hytechpro said:


> you said it compiles but on running shows nothig
> 
> is code compile sussfully?
> or showing any warnings or error...


The code compiles fine, but when i run the program it wont display the salary that am calculating.

Cout<<"salary is "<<salary; this line is not executed, thanks


----------



## jacy (Jul 19, 2004)

jacy said:


> The code compiles fine, but when i run the program it wont display the salary that am calculating.
> 
> Cout<<"salary is "<<salary; this line is not executed, thanks


Can someone please tell me whats wrong with my program. Why the salary that i calculated is not displayed when i run the program, thanks.


----------



## daveyp225 (May 13, 2003)

I'm clueless, try changing float to double and see what happens.


----------



## InterKnight (Oct 19, 2004)

Hello, jacy.

When you state that the total salary is not shown, do you mean that the screen closes without showing the end result?

I tested the code in Dev-C++, and that is what happened to me as well. The cin.get(); statement does not seem to pause the console window's closing.

If this is the case, I'll see what I can do to help; if not please let me know and I'll see if I can figure anything out.

Take care.


----------



## InterKnight (Oct 19, 2004)

jacy:

If your problem is indeed the disappearing window, you may want to take a look at this forum posting. They list various solutions to get rid of that problem:

http://www.cpp-home.com/forum/viewtopic.php?p=86464&sid=35d0b824cdfc5aa137a2873c5866cf98

I would not recommend the system("PAUSE") command listed there, though.

Hope this helps.

Take care.


----------



## jacy (Jul 19, 2004)

InterKnight said:


> jacy:
> 
> If your problem is indeed the disappearing window, you may want to take a look at this forum posting. They list various solutions to get rid of that problem:
> 
> ...


Hello Interknight

The output window is displayed when i run the programme. The ouput window asks for a sales value, and when i enter the value and hit enter the salary which is being calculated is not displayed and the window disappears.

Now when i use cin.ignore() the salary is displayed, thanks for your help interknight.

In my other programmes am just using cin.get() and it works fine. Its only in this code that i have to use cin.ignore() along with cin.get() to ignore the newline in the input stream. Could you tell me why i have to use cin.ignore () just in this code, thanks for your help.


----------



## shdunnin (May 1, 2005)

It's possible that your output buffer is not being completely flushed before program termination. In your "cout" statement, try 

cout << "Salary is: " << salaray << endl;

Also I don't see why you need the cin.get(); If you take that out, you're program will have the same effect.


----------



## jacy (Jul 19, 2004)

shdunnin said:


> It's possible that your output buffer is not being completely flushed before program termination. In your "cout" statement, try
> 
> cout << "Salary is: " << salaray << endl;
> 
> Also I don't see why you need the cin.get(); If you take that out, you're program will have the same effect.


I tried what u said, placed an endl at the end of my cout statement, and removed cin.get(). On removing cin.get() the output which is the salary is not displayed on the screen. My output window juz disappears. Therefore, i do need cin.get(). Am using Dev C++.


----------



## shdunnin (May 1, 2005)

jacy said:


> Am using Dev C++.


Why not use gcc? There is nothing syntactically with your program. Either the Dev c++ compiler is screwing up, or you have some console settings that need to be re-adjusted.


----------



## InterKnight (Oct 19, 2004)

I just do not understand what could be wrong as the syntax is non-problematic. It works just fine in Codeblocks, but not so in Dev-C++. Both IDEs use (come in default with) the MinGW GCC compiler for Windows.

I'll keep looking into this, jacy. I am sorry for my intermittend responses. I have been quite busy with college courses and physical therapy; nevertheless I have not forgotten about your situation and I am doing what I can to (hopefully) find a solution.

Take care.


----------



## jacy (Jul 19, 2004)

InterKnight said:


> I just do not understand what could be wrong as the syntax is non-problematic. It works just fine in Codeblocks, but not so in Dev-C++. Both IDEs use (come in default with) the MinGW GCC compiler for Windows.
> 
> I'll keep looking into this, jacy. I am sorry for my intermittend responses. I have been quite busy with college courses and physical therapy; nevertheless I have not forgotten about your situation and I am doing what I can to (hopefully) find a solution.
> 
> Take care.


Hello Interknight,
Thanks for your help. The link that you gave is of great help. I really appreciate your help. When i used cin.ignore() my program worked fine. Please dont waste your time looking into it any further. Good luck with your studies, take care.


----------



## jacy (Jul 19, 2004)

shdunnin said:


> Why not use gcc? There is nothing syntactically with your program. Either the Dev c++ compiler is screwing up, or you have some console settings that need to be re-adjusted.


Okay, thanks i will look into it.


----------



## InterKnight (Oct 19, 2004)

Hello again, jacy.

I am glad that the information that I provided to you was some help. No need for thanks, however. I wish I could have helped a bit more, but I am glad that I have been even a small bit of help.

If you ever need any help, please feel free to e-mail me. Hopefully I can figure out the problem you were having with Code::Blocks as it si a great IDE.

Take care.


----------



## jacy (Jul 19, 2004)

InterKnight said:


> Hello again, jacy.
> 
> I am glad that the information that I provided to you was some help. No need for thanks, however. I wish I could have helped a bit more, but I am glad that I have been even a small bit of help.
> 
> ...


Thanks Bob i appreciate your help. Take care.


----------



## CVDpr (Feb 23, 2005)

*system("pause");* ?

#include <iostream>
using namespace std;
int main ()
{
float sales;
float salary;
cout << "Enter the sales $ ";
cin >> sales;
salary = sales * 0.09 + 200;
cout << "Salary is "<< salary;

cin.get();
system("PAUSE");
return 0;

}


----------



## InterKnight (Oct 19, 2004)

I had read about using system("PAUSE") quite sometime ago. That is actually what our professor in college had us use, but I had read many articles since then that gave recommendations against using it as it calls upon the Windows system and uses that to pause the console application, thus stopping it from closing.

There are varied opinions on this method; I stopped using system("PAUSE") quite some time ago as I eventually intend to create cross-platform applications for Windows and Linux; the system("PAUSE") command (as far as I know) only works for Windows systems, so I do not want to get into the habit of using it.

That is, however, my reason for myself not using that method.

Needless to say it works well with Windows.

I'll keep looking into this as I've had some frustrations in the past with the notorious disappearing window. The only solution I know of (other than using system("PAUSE")) that would not require extra coding is to simply run the program from the command line.

But enough of my rambling...I'll keep checking on this. I wish I had more time to program as I did before...I'm beginning to get a bit rusty...

Take care.


----------



## Shadow2531 (Apr 30, 2001)

I'd probably just do it like the following. (Basically from the other thread we discussed this in.)


```
#include <iostream>
#include <limits>

using namespace std;

void pause() {
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    cout << "\n" << "Press ENTER to exit . . .";
    cin.get();
}

int main () {
    cout << "Enter the sales $";
    float sales;
    cin >> sales;
    if ( cin.good() ) {
        cout << "Salary is $" << sales * .09 + 200;
    } else {
        cout << "Input Error";
        cin.clear();
    }
    cout << endl;
    pause();
}
```
The added code to pause is not a big deal in this situation.


----------



## jacy (Jul 19, 2004)

Thanks everyone


----------



## Shadow2531 (Apr 30, 2001)

I've been investigating other ways though.

Since system("pause") is undesired because it calls another program and using cin.get() or getchar() to make a pause function is undesired (because you can't just "press *any* key to continue"), getch() needs to be used.

getch() is in conio.h on windows compilers, but there's no guarantee that you'll have conio.h.

So, what I was thinking is:

You can get rid of the conio.h depency by importing getch yourself, but only for win32 and for *nix systems, you include <ncurses.h> to use getch(). ( There are other ways I've read that you can do it on *nix with system calls, but we want to avoid that just because.)

Testing with mingw 3.4.2, ( devc++ uses mingw) here's what I have so far.

crosspause.h

```
#ifndef CROSSPAUSE_EXAMPLE
#define CROSSPAUSE_EXAMPLE

#include <stdio.h>
#include <stdlib.h>

#ifdef WIN32
    #ifdef  __cplusplus
        extern "C" {
    #endif
            _CRTIMP int __cdecl getch (void);
    #ifdef  __cplusplus
        };
    #endif
#else
    #include <ncurses.h>
#endif

inline void pause() {
    fflush(stdin);
    printf("Press any key to continue . . .");
    fflush(stdout);
    #ifndef WIN32
        initscr();
        raw();
        keypad(stdscr, TRUE);
        noecho();
    #endif
    getch();
    #ifndef WIN32
        endwin();
    #endif
    printf("\n");
    fflush(stdout);
}
#endif
```
test.c

```
#include "crosspause.h"
int main() {
    pause();
    return 0;
}
```
test.cpp

```
#include "crosspause.h"
int main() {
    pause();
}
```
I only tested that with mingw.

Now I didn't test the *nix part because I had to sacrafice my ubuntu box recently. The *nix part is taken from some ncurses.h page dealing with getch(). I'll have to find it. ( Bookmarked it in one of my million installs of Opera.)

Anyway, what the linux part is supposed to do is, put the console in raw mode where getch() will work like it does on win32. Someone will have to test that and figure and fix it if it doesn't work. (You can simulate getch on windows with setConsoleMode on windows with the windows api, but it doesn't work 100%. SetConsoleMode can put the win32 console in raw mode.)

Is it worth it to do all of this just so you can press *any* key instead of *enter*? Probably not, but something to play with.


----------



## jacy (Jul 19, 2004)

I really appreciate your effort Shadow2531, thanks a lot. I will definitely try this on my Dev C++.


----------

