c++: variable 'f' not initialized - c++

I'm creating a text-based Risk game on C++. Been trying to implement the dice / battle function but it's been giving me the error "f is used without being initialized!" Which is weird because I initialized it earlier. Here's my code
void battle(int attack, int defend)
{
int a, b, c, d, e, f;
if (attack >= 3)
{
a = rollDice();
b = rollDice();
c = rollDice();
}
else if (attack == 2)
{
a = rollDice();
b = rollDice();
c = 0;
}
else if (attack == 1)
{
a = rollDice();
b = 0;
c = 0;
}
else if (defend >= 2)
{
d = rollDice();
e = rollDice();
f = 0;
}
else if (defend == 1)
{
d = rollDice();
e = 0;
f = 0;
}
sortValues(a, b, c);
sortValues(d, e, f);
cout << endl << "The attacking country rolled the following dices: " << a
<< " " << b << " " << c << ".";
cout << endl << "The defending country rolled the following dices: " << d
<< " " << e << " " << f << ".";
if (a == d)
{
attack = attack - 1;
}
else if (a != d)
{
if (a < d)
{
attack = attack - 1;
}
else if (a > d)
{
defend = defend - 1;
}
}
else if (b == e)
{
attack = attack - 1;
}
else if (b != e)
{
if (b < e)
{
attack = attack - 1;
}
else if (b > e)
{
defend = defend - 1;
}
}
// since the int f is never used, this function is critical to implement. so that we can avoid compile time error
_unused(f);
}
int main()
{
int attack;
int defend;
int choice;
// this asks for the user choice of what he would like to do.
cout << "How large is the attacking army?" << endl;
cin >> attack;
cout << "How large is the defending army?" << endl;
cin >> defend;
while (attack >= 1 && defend >= 1)
{
cout
<< "Based on the numbers you entered, you have a couple of choices: "
<< endl << endl;
cout << "1) Battle once" << endl;
cout << "2) Battle until the end" << endl;
cout << "3) Withdraw from the battle" << endl;
cout << "Please enter your game choice (just enter the number): "
<< endl;
cin >> choice;
if (choice == 1)
{
battle(attack, defend);
}
if (choice == 2)
{
do
{
battle(attack, defend);
} while (attack != 0 && defend != 0);
cout << "Battle ended. The attacking forces are: " << attack
<< " , while the defending forces are: " << defend << endl;
}
if (choice == 3)
{
break;
}
}
if (attack < 1)
{
cout
<< "The attacking forces have lost. Defending forces keep their territory"
<< endl;
}
else if (defend < 1)
{
cout << "The attacking forces have won over the territory." << endl;
}
system("pause");
return 0;
}
This isn't all of the code, but it's the critical section which includes and uses f.

In the battle function you are calling sortValues(d,e,f), but in the first 3 if-cases f does not get a value (thus is not initialized).
Actually you should get a similar error (is it really an error or a warning?) for d and in some of the cases you do not give a value to a,b and c.

Related

Dice game code gets stuck in an infinite loop

I have a pig dice game where there are two modes (1 or 2 dice are rolled). It's played with 2 human players. When I run my program with 1 die selected it runs fine, but when I roll 2 dice it gets thrown into an infinite loop. I'm looking for a hint on where the problem lies and why it was thrown into a loop as in my mind both programs should be almost identical. Sorry in advance if the code looks strange.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int winningScore = 100;
int turn = PLAYER1;
void printIntro()
{
cout << " Welcome to the dice game: Pig! "<< endl;
cout << "The goal is to be the first player to reach 100. If playing with one die the rules are that each player can rol as many times as they choose. just dont roll a 1 or else you'll lose your turn AND all points accumulated in that round. If you're playing with 2 dies the same rules applies, but if you roll snake eyes (double 1's) you'll not only lose your turn but you'll also loose all your points. good luck and may the best player win!"<< endl;
}
int game1(string playerName, int playerScore)
{
int roll = rand() % 6 + 1;
cout << playerName << " You rolled: " << roll <<endl;
if(roll == 1)
{
cout << " OH NO! You rolled a 1. "<< endl;
cout << " Your turn is over. " << endl;
playerScore = 0;
}
else
{
playerScore +=roll;
cout << playerName << " Your score: " << playerScore <<endl;
}
if(roll == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else
{
char choice;
cout << " Would you like to roll again? (y/n): ";
cin >> choice;
if(choice != 'y')
{
if (turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
return playerScore;
}
int game2(string playerName, int playerScore)
{
int roll1 = rand() % 6 + 1;
int roll2 = rand() % 6 + 1;
cout << playerName << " You rolled: " << roll1 << " and " << roll2 <<endl;
if(roll1 || roll2 == 1)
{
cout << " OH NO! You rolled a 1. " << endl;
cout << " Your turn is over. " << endl;
playerScore = 0;
}
else if (roll1 && roll2 == 1)
{
cout << "OH CRAP! You rolled snake eyes!" << endl;
cout << " Your turn is over. " << endl;
playerScore == 0;
}
else
{
playerScore += roll1 + roll2 ;
cout << playerName << " Your score: " << playerScore <<endl;
}
if(roll1 || roll2 == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else if (roll1 && roll2 == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else
{
char choice;
cout << "Would you like to roll again? (y/n): ";
cin >> choice;
if(choice != 'y')
{
if (turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
return playerScore;
}
int main()
{
srand(time(0));
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
int dieRoll;
printIntro();
cout << " Player 1, Enter your name: ";
cin >> player1name;
cout << " Player 2, Enter your name: ";
cin >> player2name;
cout << "Wouild you like to roll with 1 or 2 dice?" << endl;
cin >> dieRoll;
if (dieRoll == 1)
{
while (player1score < winningScore && player2score < winningScore)
{
if (turn == PLAYER1)
{
player1score = game1(player1name, player1score);
}
else
{
player2score = game1(player2name, player2score);
}
}
if(player1score >= winningScore)
{
cout << player1name <<endl;
cout << " Your score is : " << player1score<<endl;
cout << player1name << " WINS! " << endl;
}
else
{
cout << player2name << endl;
cout <<" Your score: "<< player2score << endl;
cout << player2name << " WINS!" << endl;
}
}
else
{
while (player1score < winningScore && player2score < winningScore)
{
if (turn == PLAYER1)
{
player1score = game2(player1name, player1score);
}
else
{
player2score = game2(player2name, player2score);
}
}
if(player1score >= winningScore)
{
cout << player1name <<endl;
cout << " Your score is : " << player1score<<endl;
cout << player1name << " WINS! " << endl;
}
else
{
cout << player2name << endl;
cout <<" Your score: "<< player2score << endl;
cout << player2name << " WINS!" << endl;
}
}
return 0;
}
There are a couple issues in the following block which may be causing problems:
else if (roll1 && roll2 == 1)
{
cout << "OH CRAP! You rolled snake eyes!" << endl;
cout << " Your turn is over. " << endl;
playerScore == 0;
}
The way you have the conditional written, it just checks to see if roll1 is anything besides zero (that's the part before &&) and then it checks if roll2 is equal to 1. The conditional should probably read: else if (roll1 == 1 && roll2 == 1).
And I think you want to assign (=) playerScore at the bottom rather than evaluate for equality (==). You already know this, but this is what it should look like: playerScore = 0.

Game of nim: Issues with loops and program in general

I do not know how to make my program repeat without a do-while loop. The loop makes my program repeat saying "the mode is set to dumb" and "it is your turn." I could really use some help on completing this program because it is homework and due tonight at 9:30. The game is taking marbles out of a pile and the person to take the last marble loses.
// new project 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ctime>
#include <string>
#include <iostream>
#include <random>
int smartcomputer(int);
int dumbcomputer(int, int);
int playerturn(int, int);
using namespace std;
int main()
{
bool flag = true;
int marbletake = 0;
string dumborsmart;
srand(time(0));
int pilesize = rand() % (100 - 10) + 10;
cout << "Hello, Welcome to the game of Nim. Two players alternatively take marbles from a pile." << endl;
cout << "Each move a player must take at least one marble but at most half of the marbles." << endl;
cout << "Enter dumb for dumb mode and smart for smart mode." << endl;
getline(cin, dumborsmart);
do {
if (dumborsmart == "dumb") {
cout << "The mode is set to dumb." << endl;//set to dumb
bool turn = rand() % 2;
if (turn = true) {
cout << "It is your turn" << endl;
}
else {
cout << "It is the bots turn" << endl;
}
if (turn = false) { //not player's turn=false
if (dumborsmart == "dumb") {
pilesize = dumbcomputer(marbletake, pilesize);
bool turn = true;
}
else {
pilesize = smartcomputer(pilesize);
bool turn = true;
}
}
}
else {
pilesize = playerturn(marbletake, pilesize);
bool turn = false;
}
} while (pilesize != 1);
return 0;
}
int playerturn(int marbletake, int pilesize){
cout << "It is your turn. Marbles remaining in pile: " << pilesize << endl;
cout << "Enter a number of marbles to remove from the pile" << endl;
cin >> marbletake;
if (marbletake > 1 && marbletake < (pilesize / 2)) {
pilesize = pilesize - marbletake;
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You win. The bot is forced to take the last marble." << endl;
system("pause");
}
}
else {
cout << "Error, please remove a number of marbles greater than 0 or less than half the pile." << endl;
cin >> marbletake;
pilesize = pilesize - marbletake;
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You win. The bot is forced to take the last marble." << endl;
}
}
return pilesize;
}
int smartcomputer(int pilesize){
cout << "The bot is removing marbles from the pile." << endl;
if (pilesize > 63) {
pilesize = 63;
}
else if (pilesize > 31 && pilesize < 63) {
pilesize = 31;
}
else if (pilesize > 15 && pilesize < 31) {
pilesize = 15;
}
else if (pilesize > 7 && pilesize < 15) {
pilesize = 7;
}
else if (pilesize > 3 && pilesize < 7) {
pilesize = 3;
}
else {
pilesize = pilesize - rand() % (pilesize / 2) + 1;
}
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You lose. You are forced to take the last marble." << endl;
}
return pilesize;
}
int dumbcomputer(int marbletake, int pilesize){
cout << "The bot is removing marbles from the pile." << endl;
pilesize = (pilesize - rand() % (pilesize / 2) + 1); // bot takes marbles from pile(half pile size-1)
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You lose. You are forced to take the last marble." << endl;
}
return pilesize;
}
Take the cout line that prints the mode out of the loop:
cout << "The mode is set to " << dumborsmart << "\n";
do {
...
} while (pilesize != 1);
Also, if (turn = true) and if (turn = false) won't work, you need to use == there. But it's even better to write if (turn) and if (!turn).
And in your else if (pilesize > X && pilesize < Y) tests, you're skipping the cases where pilesize == Y. You should just remove && pilesize < Y, since it's already guaranteed by the fact that the previous if failed, then these boundary cases won't be skipped.

debug error heap corruption c++ pointers

so i have this assignment that tells the user to input two numbers and lets him choose some basic operations(like a calculator) from a menu.
The goal of this exercise is to help us get more familiar with pointers/pointers to functions/dynamic allocation...The professor prefers that we use the "malloc" and "calloc" commands.
There is a part I'm stuck on, the part of displaying the history. It works for the first execution, but gives a debugging error as the code proceeds. Is there anyone who can help me solve the issue?
c++ code:
#include <iostream>
using namespace std;
//structure for the history array
struct Operation{
float result;
int choice;
};
float add(float, float);
float sub(float, float);
float mult(float, float);
float divi(float, float);
int main(){
//size1=for 1st history array, size2=for 2nd history array, t=to check if an operation was done at least once
int choice,size1=0,t=0,i,size2=0;
//pointer to func
int(*operation)(int, int);
float a, b,result;
//pointers set to NULL
Operation *history1 = NULL, *history1i = NULL, *history2 = NULL, *history2i = NULL;
cout << "Enter 2 numbers a & b: ";
cin >> a >> b;
cout << "What would you like to do?" << endl << "1=Addition" << endl << "2=Subtraction" << endl << "3=Division" << endl;
cout << "4=Multiplication" << endl << "5=Display History" << endl << "6=Exit" << endl;
do{
cin >> choice;
//initialize the size of 1st array if a choice is valid
if (choice < 5 && choice >0)
size1++;
//initialize 1st array
history1 = (Operation*)calloc(size1, sizeof(Operation));
if (history1 == NULL){
cout << "History allocation failed."<<endl;
exit(0);
}
//menu
switch (choice){
case 1: result = (add)(a, b);
cout << endl << "The addition of " << a << " & " << b << " gives: " << result << endl;
break;
case 2:result = (sub)(a, b);
cout << endl << "The subtraction of " << a << " & " << b << " gives: " << result << endl;
break;
case 3:if (b != 0){
result = (divi)(a, b);
cout << endl << "The division of " << a << " & " << b << " gives: " << result << endl;
}
else
cout << "Can't perform the divison.";
break;
case 4:result = (mult)(a, b);
cout << endl << "The multiplication of " << a << " & " << b << " gives: " << result << endl;
break;
//display history
case 5:if (t == 1){
cout << endl << "Your history is: " << endl;
for (history2i = history2,i=1; (history2i - history2) < size2; history2i++,i++){
cout << "\t"<<i<<"- " << a;
if (history2i->choice == 1)
cout << " + ";
else if (history2i->choice == 2)
cout << " - ";
else if (history2i->choice == 3)
cout << " / ";
else
cout << " * ";
cout << b << " = " << history2i->result<<endl;
}
}
else
cout <<endl<< "You haven't done any calcualtions yet."<<endl;
break;
case 6:break;
default:cout << endl << "The option you entered is not valid." << endl;
break;
}
//if choice is valid
if (choice < 5 && choice >0){
//if at least one opertion executed copy the elements of history2 in history1
if (t == 1){
for (history1i = history1, history2i = history2; (history1i - history1) < size2; history1i++, history2i++){
history1i->result = history2i->result;
history1i->choice = history2i->choice;
}
history1i++;//in order to enter the new operation
}
//if no operation done
else{
history1i = history1;
}
history1i->result = result;
history1i->choice = choice;
}
//if at least one operation done=>history exists and not NULL
if (t == 1){
free(history2);//in order to create a new array with the new elements
history2 = NULL;
}
//if not operations done before increment t
if (choice < 5 && choice >0 && t == 0){
t++;
}
//if valid choice create history 2
if (choice < 5 && choice >0){
size2 = size1;
history2 = (Operation*)calloc(size2, sizeof(Operation));
if (history2 == NULL){
cout << "History allocation failed." << endl;
exit(0);
}
//store all the previous history including the new one
for (history2i = history2, history1i = history1; (history2i - history2) < size2; history2i++, history1i++){
history2i->result = history1i->result;
history2i->choice = history1i->choice;
}
}
free(history1);
history1 = NULL;
if (choice != 6)
cout << endl << "Choose another option: ";
} while (choice != 6);
return 0;
}
float add(float a, float b){
return a + b;
}
float sub(float a, float b){
return a - b;
}
float mult(float a, float b){
return a*b;
}
float divi(float a, float b){
return a / b;
}

generating linear equation with one unknown (c++)

Im sitting here for two hours and my mind is blowing. Im trying to code a generator for linear equation with one unknown. It has 3 combinations:
ax+b=c (ex. 2x-3=1)
a+bx=c (ex. 3-2x=1)
a+b=cx (ex. 4-6=2x)
So i did some simple math, made an algorithm (x=(c-b)/a), excluded zero from denominator etc. The whole point is that to make the result as integer. That's why there is this code:
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
First two combinations are working perfectly, but third one isn't. The example of an output from the third one is:
5-7=6x AND the calculated solution is x = 2. It would be x=2 if there was a + between 5 and 7. And it always goes this way. This one digit is wrong. The problem situated not in printing in the console, but in calculations.
If you have any idea, please help me to solve this.
Here is the code:
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int level_1()
{
/* LEVEL 1 - ROWNANIE Z 1 NIEWIADOMA*/
int a = 5, b = 123, c = 32;
double x;
double answer;
double licznik, mianownik;
cout << "level 1" << endl;
// losujemy kombinacje (1 z 3) dla roznorodnosci
int losowanie = 3;//= rand() % 3 + 1;
if (losowanie == 1) {
cout << "Rolled: 1" << endl << endl; // x jest przy wspolczynniku a
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
if (b > 0) cout << "+" << b;
else if (b == 0) cout << "";
else cout << b;
cout << "=";
if (c >= 0) cout << c;
else cout << c;
x = (c - b) / a;
cout << endl << "Type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
else if (losowanie == 2){
cout << "Rolled: 2" << endl << endl; // x jest przy wspolczynniku b
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
if (b!=0)
cout << b;
if (a < 0 || a == 0) cout << "";
else if (a>0) cout << "+";
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
cout << "=" << c;
x = (c - b) / a;
cout << endl << "Type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
else
{
cout << "You rolled: 3" << endl << endl; // x jest przy wspolczynniku c
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
cout << a << endl << b << endl << c << endl;
if (c != 0) cout << c;
if (b != 0)
{
if (b > 0) cout << "+";
else cout << "";
cout << b;
}
cout << "=";
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
x = ((c - b) / a);
cout << endl<< "zzz" << x << endl;
cout << endl << "type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
return 1;
}
int main()
{
//cout << y << endl;
srand(time(NULL));
while (1){
level_1();
}
}
x=(c-b)/a only holds true for equations of the first form. The other ones would be x = (c-a)/b and x = (a+b)/c. You should change your while loops accordingly.

Is there a variable, that can hold letters and decimals c++?

I am making a program, that can calculate a triangle if given some information but if I try to use "letters" or "," then, the program glitches and the program needs to be restarted. I am currently using double to hold the numbers and decimals but one of the rules with double is that you need to use "." for decimals, but i've hit the "," so much so I want to know if there is a way to fix it. I also want to know if the person can choose to write both numbers and letters in the same box.
#include <iostream>
#include <windows.h>
#include <math.h>
#define PI 3.14159265359
using namespace std;
void SetColor(int ForgC);
int failure = 0;
int wait;
double a, b, c, A, B, C = 90;
int main()
{
SetColor(10);
{
info:
while (GetAsyncKeyState(VK_RETURN))
{
}
system("cls");
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "A = ";
cin >> A;
cout << "B = ";
cin >> B;
while (GetAsyncKeyState(VK_RETURN))
{
}
}
{
calculate:
if (a == 0 && b == 0 && c == 0 && A == 0 && B == 0)
{
cout << "There need to be at least two numbers!\n" << endl;
system("pause");
failure = 1;
} // Failsafe
else if (c != 0)
{
if (a >= c || b >= c)
{
cout << "a or b cannot be greater than c!\n" << endl;
system("pause");
failure = 1;
}
} // Failsafe 2
else if (A >= 90 || B >= 90)
{
cout << "A or B cannot be equal to, or greater than 90!\n" << endl;
system("pause");
failure = 1;
;
} // Failsafe 3
if (a != 0 && c != 0)
{
b = sqrt(c * c - a * a);
A = asin(a / c) * 180 / PI;
B = 90 - A;
}
else if (b != 0 && c != 0)
{
a = sqrt(c * c - b * b);
A = acos(b / c) * 180 / PI;
B = 90 - A;
}
else if (a != 0 && b != 0)
{
c = sqrt(a * a + b * b);
A = atan(a / b) * 180 / PI;
B = 90 - A;
}
else if (c != 0 && A != 0)
{
a = c * sin(A);
b = c * cos(A);
B = 90 - A;
}
else if (c != 0 && B != 0)
{
A = 90 - B;
a = c * sin(A);
b = c * cos(A);
}
else if (a != 0 && B != 0)
{
b = a * tan(B);
c = sqrt(a * a + b * b);
A = 90 - B;
}
else if (b != 0 && A != 0)
{
a = b * tan(A);
c = sqrt(a * a + b * b);
B = 90 - A;
}
else if (a != 0 && A != 0)
{
b = a / tan(A);
c = sqrt(a * a + b * b);
B = 90 - A;
}
else if (b != 0 && B != 0)
{
a = b / tan(B);
c = sqrt(a * a + b * b);
A = 90 - B;
}
if (failure == 1)
{
goto failsafe;
}
goto answer;
}
{
failsafe:
failure = 0;
system("cls");
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "A = ";
cin >> A;
cout << "B = ";
cin >> B;
while (GetAsyncKeyState(VK_RETURN))
{
}
goto calculate;
}
answer:
while (1)
{
system("cls");
wait = 1;
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "\nDo you want to write new information? (";
SetColor(9);
cout << "Y";
SetColor(10);
cout << "/N)";
while (wait == 1)
{
if (GetAsyncKeyState(VK_RIGHT))
{
wait = 0;
}
else if (GetAsyncKeyState(VK_RETURN))
{
goto info;
}
}
system("cls");
wait = 1;
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "\nDo you want to write new information? (Y/";
SetColor(9);
cout << "N";
SetColor(10);
cout << ")";
while (wait == 1)
{
if (GetAsyncKeyState(VK_LEFT))
{
wait = 0;
}
else if (GetAsyncKeyState(VK_RETURN))
{
return 0;
}
}
}
}
void SetColor(int ForgC)
{
WORD wColor;
// We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
// We use csbi for the wAttributes word.
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
// Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
You need to add error handling.
If input fails, you need to decide what to do. E.g. you can print an error message, clear the stream state and start over:
std::cin.exceptions(std::ios::failbit);
try
{
std::cout << "a = ";
std::cin >> a;
std::cout << "b = ";
std::cin >> b;
std::cout << "c = ";
std::cin >> c;
std::cout << "A = ";
std::cin >> A;
std::cout << "B = ";
std::cin >> B;
} catch(std::exception const& e)
{
std::cerr << "Error in input.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
goto info;
}
May I heartily suggest not using goto for program flow (use loops). And you may also use non-exception error checking:
std::cout << "a b c A B = ";
if (!(std::cin >> a >> b >> c >> A >> B))
{
if (std::cin.eof())
{
std::cerr << "Goodbye\n";
exit(1);
}
std::cerr << "Error in input.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
goto info;
}
PS. For the numeric_limits traits, include
#include <limits>
Apart from the usual tricky error handling with text input, in particular on the console, and the funny gotos: In order to change number formats (and others as well) you probably need to "imbue a locale" on cin (and, for consistency, on cout). I have never done it but this site has examples which look reasonable and simple: http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_101.html