0

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.

monopolo11
  • 57
  • 1
  • 8
  • 1
    Please indent and comment the code so you (and we) can see whatever it is you're doing. – Potatoswatter Sep 10 '15 at 03:36
  • 1
    You have not given useful information. What do you mean by "have a decimal point"? What data are you entering? What do you mean by "not adding"? Describe the behaviour you expect, and describe how the behaviour you observe is different. Don't rely on people who read your post being able to divine your intent if you describe it unclearly. – Peter Sep 10 '15 at 03:47
  • Done, i think, i just explained most of the code – monopolo11 Sep 10 '15 at 03:50
  • Indicate the input file you are using. If `signo` isn't given either a `+` or `-` this will loop indefinitely. – Adrian Sep 10 '15 at 03:51
  • why are you removing the indentation again? – phuclv Sep 10 '15 at 03:52
  • Also, reindent your code. It is unreadable. – Adrian Sep 10 '15 at 03:52
  • I'm not editing that again. The indentation on this is once-again terrible. – WhozCraig Sep 10 '15 at 03:52
  • @Adrian, thats a new problem that i hadnt realized, but i think that is not the problem im trying to solve, but i can fix it with if(signo!="+"||signo!="-"){cout< – monopolo11 Sep 10 '15 at 03:52

2 Answers2

1

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.

Community
  • 1
  • 1
Thehx
  • 58
  • 3
  • Yes, given that this seems to be from a Spanish speaking country, it is quite possible that a comma is what is wanted and not a decimal point. – Adrian Sep 10 '15 at 03:57
0

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:

Community
  • 1
  • 1
Adrian
  • 8,385
  • 2
  • 32
  • 83