I Hate C++... No Really Need Some Help


Recommended Posts

ok first I would like to thank jcl for all the help.. FI you have a site I can send a donation to help pay for this training..

I am tring to make a calculator ... now I know a simple way to do it.. but I'm thick headed and I want to do a lot of diffrent things likecheck for correct input and stuff. I must be tired but this is my start..

the compiler does not like my cin.get() with a char.. ??

here we go.. to all the brave souls

hint the end goal is to create a text based calculator that will act just like a gui one.. you type a number type function keep going till you hit enter and it gives a final answer.. I know i am not close but if anyone can point out errors off the bat I can go back and focuse on making a working then make it look how I want latter.

by the way this is not school work , this is me tring to make sense of what the book and jcl has tought me so far..

//text baased calcultor program
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

//standard protability between windows and unix calls..
//use the -DUNIX or -DWIN32 option for gcc
void clear_screen()
{
#if defined WIN32
       system("cls");
#elif defined UNIX
       system("clear");
#endif
}

//input prompt and type checking
double Number_Prompt ( const string & s)
{
        while (true) {
        double PNumber;
        cout << s << flush;
        cin >> PNumber;
        if (cin.fail()) {
        cerr << "invalid input \n";
        cin.clear();
        cin.ignore();
}
       else
       return PNumber;

 }
}

// input prompt and type checking
double sNumber_Prompt ( const string& t)
{
        while (true) {
        double sNumber;
        cout << t << flush;
        if (cin.fail()) {
        cerr << "invalid input \n";
        cin.clear();
        cin.ignore();
       }
       else
       return sNumber;
}
}

void help_prompt()
{
       int a;
       clear_screen();

Edited by iccaros
Link to post
Share on other sites
ok first I would like to thank jcl for all the help.. FI you have a site I can send a donation to help pay for this training..

Heh, no thanks. I'm really enjoying this, I don't want to ruin it by making a job :)

Anyway, did the board eat the rest of the post?

Link to post
Share on other sites
Anyway, did the board eat the rest of the post?

LOL... or my dog did....

I know the case statments are not done.. but even if I change type = cin.get() to cin >> type;

the program ends with 0 as a answer

:D

bash-2.05b$ cat cal.cpp
//text baased calcultor program
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

//standard protability between windows and unix calls..
//use the -DUNIX or -DWIN32 option for gcc
void clear_screen()
{
#if defined WIN32
       system("cls");
#elif defined UNIX
       system("clear");
#endif
}

//input prompt and type checking
double Number_Prompt ( const string & s)
{
        while (true) {
        double PNumber;
        cout << s << flush;
        cin >> PNumber;
        if (cin.fail()) {
        cerr << "invalid input \n";
        cin.clear();
        cin.ignore();
}
       else
       return PNumber;

 }
}

// input prompt and type checking
double sNumber_Prompt ( const string& t)
{
        while (true) {
        double sNumber;
        cout << t << flush;
        if (cin.fail()) {
        cerr << "invalid input \n";
        cin.clear();
        cin.ignore();
       }
       else
       return sNumber;
}
}

void help_prompt()
{
       int a;
       clear_screen();
       cout << "Valid Funtions are *-/+" << endl;
       cout << "press any key to contenue" << endl;
       a = cin.get();
       clear_screen();
               }

int main ()
{
       clear_screen();
       double PNumber, sNumber, Accum = 0, holder;
       char type;

       PNumber = Number_Prompt ("enter number: ");
       cout << "enter Function or h for help: ";
       while (type = cin.get() != "e"){  // it no like this??
       switch (type)
       {

       case '+':
       sNumber = sNumber_prompt ("Enter Number: ");
       holder = PNumber + sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;
       break;

       case '-':
       holder = PNumber - sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;
       break;

       case '/':
       if (sNumber == 0 ){
       cout << "devide by zero error";
       break;}
       holder = PNumber / sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;

       break;

       case '*':
       holder = PNumber * sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;

       break;

       default:
       cout << "You did not enter a correct function" << endl;
       break;
       }
       clear_screen();
       cout << Accum << endl;

       return 0;
}
}

Link to post
Share on other sites

This expression

type = cin.get() != "e"

should probably be

(type = cin.get()) != 'e'

The parenthesis are necessary because = has lower precedence than !=, so the first form is identical to

type = (cin.get() != 'e')

the variable 'type' is assigned the value of the comparison (0 or 1).

The single quotes are necessary because you want the character 'e' and not the string "e". In the first form you're comparing the character returned by get() to the address of the string literal "e".

Also, get() returns an int rather than char, presumably because it can return traits::eof to signal EOF and the value traits::eof may not be in the range of char (this is a guess, I only skimmed the standard, but it would consistent with C's fgetc(), getchar(), etc). Doesn't make much difference since you aren't checking for EOF anyway.

You also probably want to use cin.ignore() after cin.get(). When the user enters the function at least two characters are placed on the input stream: the function character ('+', '-', etc) and a newline generated by the user hitting Enter. get() pulls off the function but leaves the newline, so you use ignore() (or something similar) to throw it away. (That will of course break if the user enters two consecutive characters on one line.)

The same newline issue comes up in the two prompt functions. In those operator>>() pulls an integer or double off the input stream but leaves the newline. Same solution, same problem with consecutive tokens.

Finally, you left out the line

cin >> sNumber;

in sNumber_Prompt().

So, patching sNumber_Prompt and changing this block of code

       PNumber = Number_Prompt ("enter number: ");
      cout << "enter Function or h for help: ";
      while (type = cin.get() != "e"){  // it no like this??

to this

    PNumber = Number_Prompt ("enter number: ");
   cin.ignore();
   cout << "enter Function or h for help: ";
   while ((type = cin.get()) != 'e'){  // it no like this??
       cin.ignore();

should produce the correct results

$ g++ foo.cc
$ ./a.out
enter number: 2
enter Function or h for help: +
Enter Number: 2
4
4

Edited by jcl
Link to post
Share on other sites

ok I did some changes adn it works 80%

I had issues. but I deleated the post as it was a bad post anyways..

here is what I have now..

now I just need ot clean up input output to make it usefull

thanks for the help..

bash-2.05b$ cat cal.cpp
//text baased calcultor program
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

//standard protability between windows and unix calls..
//use the -DUNIX or -DWIN32 option for gcc
void clear_screen()
{
#if defined WIN32
       system("cls");
#elif defined UNIX
       system("clear");
#endif
}

//input prompt and type checking
double Number_Prompt ( const string& s)
{
        while (true) {
        double PNumber;
        cout << s << flush;
        cin >> PNumber;
        if (cin.fail()) {
        cerr << "invalid input \n";
        cin.clear();
        cin.ignore();
}
       else
       return PNumber;

 }
}


void help_prompt()
{
       int a;
       clear_screen();
       cout << "Valid Funtions are *-/+" << endl;
       cout << "press any key to contenue" << endl;
       a = cin.get();
       cin.ignore();
       clear_screen();
               }

int main ()
{
       clear_screen();
       double PNumber, sNumber, Accum = 0, holder;
       char type;

       bool RunLoop = true;
       while(RunLoop) {
       PNumber = Number_Prompt ("enter number: ");
       cin.ignore();
       cout << "enter Function or h for help: ";
        type = cin.get();

       switch (type)
       {

       case '+':
       sNumber = Number_Prompt ("Enter Number: ");
       cin.ignore();
       holder = PNumber + sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;
       break;

       case '-':
       sNumber = Number_Prompt ("Enter Number: ");
       holder = PNumber - sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;
       break;

       case '/':
       sNumber = Number_Prompt ("Enter Number: ");
       if (sNumber == 0 ){
       cout << "devide by zero error";
       break;}
       holder = PNumber / sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;

       break;

       case '*':
       sNumber = Number_Prompt ("Enter Number: ");
       holder = PNumber * sNumber;
       Accum += holder;
       clear_screen();
       cout << Accum << endl;

       break;

       case 'e':
       case 'E':
       RunLoop = false;
       break;

       case 'h':
       case 'H':
       help_prompt();
       break;

       default:
       cout << "You did not enter a correct function" << endl;
       break;
       }
}


       return 0;

}
bash-2.05b$                  

Edited by iccaros
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...