Author Topic: C++ Issue with getline (silly me again)  (Read 506 times)

I'm still getting used to C++, so I have no idea what I could be doing wrong exactly.

I was trying to make a simple little calculator in C++, however I had a small problem where after it asks for your operator, it just skips it instantly, and if I add in a wait time, it doesn't even let me type the operator in. What am i doing wrong?


Code: [Select]
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
using namespace std;

void wait(int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main()
{
string name;
int currentNumber;
int numberone;
int numbertwo;
bool finished = false;

while(finished != true)
{
cout << "Enter a number: ";
cin >> numberone;
wait(1);
cout << "\nEnter a second number: ";
cin >> numbertwo;
wait(1);
cout << "\nEnter an operator: ";
getline(cin, name);
if(name == "+")
currentNumber = numberone + numbertwo;
else if(name == "-")
currentNumber = numberone - numbertwo;
else if(name == "*")
currentNumber = numberone * numbertwo;
else if(name == "/")
currentNumber = numberone / numbertwo;
else
cout << "\nError in processing the expression.\n"; continue;
cout << "\nAnswer is: " << currentNumber << ".\n";
}
return 0;
}
« Last Edit: September 25, 2011, 10:50:40 PM by otto-san »


new issue
Issue #2 - EXCLUSIVE INTERVIEW WITH #include

GENERAL NICK FANCLUB

Why the bloddy hell are you using getline anyway?


that goes back to where i decided to learn C++ from

Why? What would be better?
Let me just rewrite the program first.
Then I'll awnser whatever questions you have.
One sec.

Code: [Select]
#include <iostream>
using namespace std;

int main()
{
string name;
int currentNumber;
int numberone;
int numbertwo;
bool finished = false;
        bool show = true;

        cout << "\nEnter a number: ";
cin >> numberone;
cout << "\nEnter a second number: ";
cin >> numbertwo;
cout << "\nEnter an operator: ";
        cin  >> name;

if(name == "+")
currentNumber = numberone + numbertwo;
else if(name == "-")
currentNumber = numberone - numbertwo;
else if(name == "*")
currentNumber = numberone * numbertwo;
else if(name == "/")
currentNumber = numberone / numbertwo;
else
        {
cout << "\nError in processing the expression.\n";
                show = false;
         }

         if(show)
         cout << "\nAnswer is: " << currentNumber << ".\n";

       cout<<"\nEnter anything to quit.\n";
       cin>>name;

return 0;
}

I took out the loop part, but that's not really the point of what I was fixing.
The waiting and use of getline is not nessacary, I believe.
Granted I did not test it.
« Last Edit: September 25, 2011, 10:49:00 PM by DrenDran »

Let me just rewrite the program first.
Then I'll awnser whatever questions you have.
One sec.
Okay. I feel kind of silly for asking something like this since it seems like such a small problem.

I used cin instead and it seems to work now.
« Last Edit: September 25, 2011, 10:48:52 PM by otto-san »

i dont think you can use "wait" in c++
i think you can only use "sleep(INSERTTIMEINMSHERE);"

i dont think you can use "wait" in c++
i think you can only use "sleep(INSERTTIMEINMSHERE);"
wait was defined