Loop my program?

Hey guys, this is my second post. I am programming a simple K value calculator.
What would be the most essential way to clear the terminal and start the program all over again? Thankful for any answers!

My code:

#include <iostream>
#include <cmath>
using namespace std;

int main() {

double firstX;
double firstY;
double secondX;
double secondY;
double kphase1;
double kphase2;
double kVal;
double kValMultiply;
double mVal;
double substractY;
double substractK;
double yVal;
double addY;
double addK;
string userInput;


cout << "K-value calculator by Alexander Ensani TE14B" << endl;
cout << "" << endl;

cout << "Type in the first coordinate pair" << endl;
cout << "" << endl;

cout << "Enter the value of the first X: ";
cin >> firstX;
cout << "Enter the value of the first Y: ";
cin >> firstY;
cout << "" << endl;

cout << "Type in the second coordinate pair" << endl;
cout << "" << endl;

cout << "Enter the value of the second X: ";
cin >> secondX;
cout << "Enter the value of the second Y: ";
cin >> secondY;
cout << "" << endl;

kphase1 = secondY - firstY;
kphase2 = secondX - firstX;
kVal = kphase1 / kphase2;

cout << "The first coordinate pair is: (" << firstX << "," << firstY << ")" << endl;
cout << "The second coordinate pair is: (" << secondX << "," << secondY << ")" << endl;
cout << " " << endl;

cout << "I will calculate the value of K with the following formula: ";
cout << secondY << " - " << firstY << " / " << secondX << " - " << firstX << endl;
cout << "" << endl;

cout << "The value of K is: " << kVal << endl;

cout << "Would you like to know what the value of M is? Type Y or N" << endl;
cin >> userInput;
cout << " " << endl;

if (userInput == "y") {

cout << "I will try to solve the following equation: ";
cout << firstY << " = " << kVal << " * " << firstX << " + M" << endl;
cout << "" << endl;

kValMultiply = kVal * firstX;

substractK = kValMultiply - kValMultiply;
substractY = firstY - kValMultiply;

addK = kValMultiply + kValMultiply;
addY = firstY + kValMultiply;

if (kValMultiply > 0) {

cout << "The equation results: ";
cout << substractY << " = " << substractK << " + M" << endl;
cout << "" << endl;
cout << "The value of M is: " << substractY << endl;

}

if (kValMultiply < 0) {

cout << "The equation results: ";
cout << addY << " = " << addK << " + M" << endl;
cout << "" << endl;
cout << "The value of M is: " << addY << endl;

}
}

else if (userInput == "n") {
cout << " " << endl;
cout << "Okay, we will leave it like this!" << endl;
cout << "" << endl;
} else {

cout << "Invalid input, please type Y (Yes) or N (No)." << endl;



}
}
There is a discussion of clearing the screen here:
http://www.cplusplus.com/articles/4z18T05o/

start the program all over again

Use a loop.

Suggestion: Move your code to a function, then put your loop in main().
1
2
3
4
  while (true)
  {  calculate_k_value ();
      // clear screen by whatever method
  }


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.
Put your code in a Do-while-loop and all will be good :)


Ive made 2 changes. I just added this - if (userInput == "y" || userInput == "Y")
And the same with the "n". You know, incase the user types Y instead of y. because the program asks the user to write an uppercase Y so this made more sense.

Here is the full code - http://pastebin.com/t1LBC5Sq
Last edited on
Damn it, you guys have been so helpful. Thanks alot!!
Topic archived. No new replies allowed.