Reputation: 57
I'm trying to make a program that "Tabulates", a formula to make simple graphics. The problem that I have is that some variables are not adding if they have a decimal point, that makes it run in an infinite loop, I want to fix this but I don't know how.
Here is the code:
#include <iostream>
//#include <conio.h>
#include <iomanip>
#include <limits>
//#include <ncurses.h>
using namespace std;
int main()
{
string formula;
float a;
float b;
float c;
float cantidad1;
float cantidad2;
float intervalo;
intervalo=1;
string signo;
cout << "Formula: y=ab+/-c\n";
cout << "Introduce el valor de a\n";
cin >> a;
//cout << "Introduce el valor de b\n";
//cin >> b;
cout << "Introduce el valor de c\n";
cin >> c;
cout << "Es suma o resta (responde con + o -)\n";
cin >> signo;
cout << "Del:";
cin >> cantidad1;
cout << "Al:";
cin >> cantidad2;
cout << "Intervalo:";
cin >> intervalo;
cout << "x|y\n";
cout << "----\n";
b=cantidad1;
while(cantidad1 <= cantidad2){
float res1 = 0;
if(signo=="-"){
res1 = a*b-c;
b=b+intervalo;
cantidad1= cantidad1+intervalo;
};
if(signo=="+"){
res1 = a*b+c;
b=b+intervalo;
cantidad1= cantidad1+intervalo;
};
cout<< b << "|" << res1 << "\n";
};
}
And besides that i would like to add a "Press any key to continue", but the methods that I've tried getch()
didn't work.
Upvotes: 0
Views: 110
Reputation: 10911
The problem is most likely that an invalid character has crept into your stream. This will result in the error bit to be set. Until these bits have been cleared (using ios::clear()
), you will not be able to read in from the stream. This will look like it is in an infinite loop, outputting prompts, but skipping inputs.
See this answer for more info on how to fix the problem:
Upvotes: 0
Reputation: 58
Could it be that you are entering a comma instead? If so, check out this answer.
As for the last part of the question, this "Press any key to continue" answer might help too.
Upvotes: 1