Skipping Error - Program Does Not Restart from The Beginning

I try to make the program restarts when character y is entered. It jumps over the first and asking for the second variable input except on the first run. Is there anything to do with my pointer because y is counted as an empty space input? I tried many different ways to reset my pointer value but to no avail.

Here is my code
https://onlinegdb.com/BkUkjOa17
You probably have an errant carridge return in your input buffer. Flush your buffer like you do with cin.ignore() on Line 52.
indent your code

1
2
3
4
		if(c == 'y') {
			delete[] array; //¡¿?!
			restart = true;
		}
`array' becomes invalid there, but you try to use it again inside the loop.

show an example input/output
Thz a lot Computergeek01 (5531). It works nicely, i thought it has something to do with pointer deference. By the way, what is an errant carridge return ?
I am learning c++, so let me know if you have a better idea.
I have added some codes and made comments.

#include <iostream>
#include <cctype>
#include <string>
#include <math.h>
//#include "koolplot.h"
#include <cstdlib>
using namespace std;


//This program plots a parabolic function for any input of variables//

void printheader () //Print the header
{

cout << "Please Enter A,B and C for the parabolic function below" << endl;

cout << "y=Ax^2+Bx+C" << endl;

}


int getInt (int *x, bool &b)
{ //check for non-integer input

string theInput;

int inputAsInt;

getline (cin, theInput);
while (cin.fail () || cin.eof ()
|| theInput.find_first_not_of ("-0123456789.") != std::string::npos)
{
// it the enter 'y', break it, otherwise, it will go to "Invalid input. Tye again"
if (theInput == "y") {
b = false; // pass out reference
break;
}

cout << "Invalid input. Try again: ";

if (theInput.find_first_not_of ("-0123456789. ") == std::string::npos)
{

cin.clear ();

cin.ignore (1000, '\n');

}
getline (cin, theInput);

}

std::string::size_type st;

inputAsInt = atoi (theInput.c_str());//Turn string into int

*x = inputAsInt;

return *x;

}




/*int MathRun (int A, int B, int C,int a, int b) //plot star points for each result
{
if (A && B && C && a && b != 0)
{

plotdata x(a, b), y=A*x*x+B*x+C;
plot(x,y);
return 0;
}

}*/

int main () //ask for variables of parabolic function
{


bool restart = true;
bool b = true;

int *array;

array = new int[5];

printheader ();

while(restart){
for (int y = 0; y <= 4; y++)
{

char NameOfVariable[] = "ABC";
if (y==3)
{
cout<<"Please Enter Plotting Range for "<<endl;
cout <<"X1"<< ":";
getInt (&array[3], b);
if (b == false) {
break;
}
}
else {
if (y == 4) {
cout << "Please Enter Plotting Range for" << endl;
cout << "X2" << ":";
getInt(&array[4], b);
if (b == false) {
break;
}
} else {
cout << "Please Enter An Integery for Variable" << endl;
cout << NameOfVariable[y] << ":";
getInt(&array[y], b);
if (b == false) {
break;
}

}
}

int VariableA = array[0];

int VariableB = array[1];

int VariableC = array[2];

int Xrange1 = array[3];

int Xrange2 = array[4];

//MathRun (VariableA, VariableB,VariableC ,Xrange1,Xrange2);

}

cout<<"Do you want to restart the program again? y/n"<<endl;
b = true;
char c;
cin>>c;
std::cout << " waht is c = " << std::endl;
if (c =='y'){
std::cout << "it is y " << std::endl;
// reset the array, before running the while loop again, it will crash otherwise.
array[0] = 0;
restart = true;
}
else{
std::cout << "it is not y " << std::endl;
restart = false;
}

}
return 0;
}


Topic archived. No new replies allowed.