2D array tic tac toe - What am I doing wrong? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Having problems getting my game to work... Any thoughts on what I might do? I have gone through most of the code and initialized it, but for some reason it is saying for most of the function that I don't have the proper points. Also that the drawBoard doesn't have the void type. Please help
Thanks
#include <iostream>
using namespace std;
// initialize a 2d board (3,3) with asterisk
void drawBoard(char board[][]);
char check_Winner(char [][]);
int main(){
char board[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};
bool win = false;
bool in_row = false;
bool in_col = false;
int row;
int column;
// run a loop:
while (!win) {
// Display the content of the board
drawBoard(char board[][]));
cout << "Player 1's turn:";
// Ask player one to chose a location (row, column)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
// check if move is valid
//check if location has astericks and its between 0 thru 2
bool valid = false;
while (!valid)
{
//check if the move is within range.
if(!(row <= 3 && row >= 1 && column <= 3 && column >= 1)){
cout << "Error: Either the row or column is not within range of the board.\n";
// ask again (error: row in column not within range)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}
else if(board[row-1][column-1] != '*')
{
cout << "Error: The (row,column) is already occupied.\n";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}else {
valid = true;
}
}
board[row-1][column-1] = 'X';
drawBoard(char board[][]);
// Check if someone won or if there is a tie
check_Winner(char board[][]);
// Ask player two to chose a location (row, column)
cout << "Player 2's turn:";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
// check if move is valid
//check if location has astericks and its between 0 thru 2
bool valid = false;
while (!valid)
{
//check if the move is within range.
if(!(row <= 3 && row >= 1 && column <= 3 && column >= 1)){
cout << "Error: Either the row or column is not within range of the board.\n";
// ask again (error: row in column not within range)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}
else if(board[row-1][column-1] != '*')
{
cout << "Error: The (row,column) is already occupied.\n";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}else
{
valid = true;
}
board[row-1][column-1] = 'O';
}
drawBoard(char board[][])
// Check if someone won or if there is a tie
check_Winner(char board[][]);
}
system("pause");
return 0;
}
char check_Winner(char board[][]){
char winner = 'T';
// Checks for horizontal:
for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
winner = board[i][0];
// Checks for vertical:
for (int i = 0; i < 3; i++)
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
winner = board[0][1];
// Checks for diagnol:
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
winner = board[1][1];
// checking the result:
switch (winner) {
case 'T': cout << "IT'S A TIE";/* tie */ break;
case 'X': cout << "PLAYER 1 WON!";/* X won */ break;
case 'O': cout << "PLAYER 2 WON!";/* O won */ break;
default : cout << "NEXT PLAYER'S TURN...>"; continue;
}
}
void drawBoard(char board[][])
{
cout << " 1 2 3" << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 3" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
}

I pasted your code into codepad.org and compiled it. The first error message is:
Line 6: error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
In other words - if you don't tell the compiler something about the dimensions of your 2-D array, it won't know how to find an element. Why is that? Well, when you access a 2D array of m x n elements, a[i][j], you are really computing
*(a + n * i + j)
because for each time you increment the row i, you skip another n elements.
Another way to think of this - the "2D" elements are really one big block. If you had
0 1 2 3
4 5 6 7
8 9 10 11
They are really stored as
0 1 2 3 4 5 6 7 8 9 10 11
And you find element [1][1], say, by computing the offset (1D index, if you like) 1 * 4 + 1 = 5. And sure enough, element 5 in the linear array has the value 5, which is what you would have found by going one down and one across.
We fix this first compiler error by replacing
void drawBoard(char board[][]);
char check_Winner(char [][]);
with
void drawBoard(char board[][3]);
char check_Winner(char [][3]);
(as an aside, I'm not sure why check_Winner is defined as returning a char (you don't have a return Winner; statement in your function...) , and why you use the strange combination of underscores and caps in your function name. We'll get back to that later)
Next, the compiler complains about the way you call these functions. You need to call them (in line 23 and elsewhere) not as
drawBoard(char board[][]);
but simply as
drawBoard(board);
Since you already have defined that you are passing a char, and you don't need the [][] to remind the compiler of the type of board (you defined it earlier).
Next error (after these are fixed) that the compiler throws is
In function 'int main()':
Line 72: error: redeclaration of 'bool valid'
Indeed, you defined
bool valid = false;
in line 33. After that, when referring to the same variable, you MUST NOT put bool in front of the variable name - it looks like you are (re)declaring the variable. Just put
valid = true
in line 72. (Aside - you clearly did a copy-paste of a big block of code, which is how this error crept in. You want to think hard about finding a way to make "make a move" a single function that can be called for either player 1 or player 2 - then you don't need to repeat all this code, and things will look cleaner. And less prone to mistakes.).
With that fixed, the compiler complains about line 99:
In function 'int main()':
Line 99: error: expected ';' before 'check_Winner'
As is often the case with errors like this, the problem is in the line before. Line 97:
drawBoard(board)
is missing a semicolon at the end, and should be
drawBoard(board);
With that fixed, the compiler complains about line 130:
default : cout << "NEXT PLAYER'S TURN...>"; continue;
In function 'char check_Winner(char (*)[3])':
Line 130: error: continue statement not within a loop
The continue statement is used to say "go straight to the next iteration of the loop you are in" (while, for...). But you are using it inside a switch statement, and there is no loop (within the scope of the function you are in) that the code can jump to. If you just leave the continue; off, the code will continue just fine.
In fact (although this is not really what you are asking about), the break' will not cause the program to stop; it will just take you to the end of the function, and the code will keep going. This is why you really need a return value in your check_Winner function - and you need to check it after you have called it, so you can take appropriate action (print message, and quit the game).
Taking out the continue on line 130, we now hit a warning on line 133:
In function 'char check_Winner(char (*)[3])':
Line 133: warning: control reaches end of non-void function
This is saying "you declared this function to return a char, but you forgot to put in a return statement!". Add
return winner;
before the end of the function, and the complaint goes away.
The result of all these edits is the following code. I'm not saying it is "good" code, but at least the compiler errors have been removed. I hope that the process that I described will be helpful for you as you learn. Most compilers have flags to enable warnings and error messages; I strongly recommend that you always enable every warning you can, and pay attention to what the compiler is complaining about. You will learn to write better code..
disclaimer - I was writing this whilst "on the road" so I could only use codepad.org for basic debugging. It doesn't allow interactive programming so I could not test whether the final code 'works' - I can only tell you that it stopped complaining about the errors in the code... I am 99% sure that your check_Winner code has some logic errors in it - for one, you start out with
winner = 'T';
Which means you will call a tie unless one of the other conditions is met; I think you should only call a tie if you have no asterisks left (if you want to be really clever you can call a tie when there is no possible solution but that's a much harder problem to code).
Anyway - here is the code that compiles. There is more to do before you have a working game, I think...
#include <iostream>
using namespace std;
// initialize a 2d board (3,3) with asterisk
void drawBoard(char board[][3]);
char check_Winner(char board[][3]);
int main(){
char board[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};
bool win = false;
bool in_row = false;
bool in_col = false;
int row;
int column;
// run a loop:
while (!win) {
// Display the content of the board
drawBoard(board);
cout << "Player 1's turn:";
// Ask player one to chose a location (row, column)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
// check if move is valid
//check if location has astericks and its between 0 thru 2
bool valid = false;
while (!valid)
{
//check if the move is within range.
if(!(row <= 3 && row >= 1 && column <= 3 && column >= 1)){
cout << "Error: Either the row or column is not within range of the board.\n";
// ask again (error: row in column not within range)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}
else if(board[row-1][column-1] != '*')
{
cout << "Error: The (row,column) is already occupied.\n";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}else {
valid = true;
}
}
board[row-1][column-1] = 'X';
drawBoard(board);
// Check if someone won or if there is a tie
check_Winner(board);
// Ask player two to chose a location (row, column)
cout << "Player 2's turn:";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
// check if move is valid
//check if location has astericks and its between 0 thru 2
valid = false;
while (!valid)
{
//check if the move is within range.
if(!(row <= 3 && row >= 1 && column <= 3 && column >= 1)){
cout << "Error: Either the row or column is not within range of the board.\n";
// ask again (error: row in column not within range)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}
else if(board[row-1][column-1] != '*')
{
cout << "Error: The (row,column) is already occupied.\n";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}else
{
valid = true;
}
board[row-1][column-1] = 'O';
}
drawBoard(board);
// Check if someone won or if there is a tie
check_Winner(board);
}
system("pause");
return 0;
}
char check_Winner(char board[][3]){
char winner = 'T';
// Checks for horizontal:
for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
winner = board[i][0];
// Checks for vertical:
for (int i = 0; i < 3; i++)
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
winner = board[0][1];
// Checks for diagnol:
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
winner = board[1][1];
// checking the result:
switch (winner) {
case 'T': cout << "IT'S A TIE";/* tie */ break;
case 'X': cout << "PLAYER 1 WON!";/* X won */ break;
case 'O': cout << "PLAYER 2 WON!";/* O won */ break;
default : cout << "NEXT PLAYER'S TURN...>";
}
return winner;
}
void drawBoard(char board[][3])
{
cout << " 1 2 3" << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 3" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
}
Postscript.
I am not a C++ expert by any stretch of the imagination. But people who are tend to insist not to use using namespace std;, and instead to write
std::cout << "hello world" << std::endl;
etc. As your code gets more complicated, being explicit about what function/constant belongs to what class gets more important. So I have been told... See Why is "using namespace std" considered bad practice? . Both the question and the answer got a LOT of upvotes... suggesting this is an important thing that people think about. Do read beyond the accepted answer... there are some gems further down that page.

May I suggest you to try to use a typedef for your board?
typedef char tttboard[3][3];
your board declaration:
tttboard board={{'*','*','*'},{'*','*','*'},{'*','*','*'}};
your functions will look like: (by reference!)
void drawBoard(tttboard& board);
char check_Winner(tttboard& board);
And as you been told in the comment you call it without the char and [][] like this:
drawBoard(board);
check_Winner(board);

Related

Validating user input as coordinates (A1...)

I am struggling to find a way to break down an input from the user to a location on a chess board. For example (A1). I want to make sure that they have entered a Letter between A and H and a number between 1 and 8. Not sure if comparing ASCII is the best approach?
Using C++ this is a snippet of what I have attempted. startingp is the input from the user
char startChar = startingp[0];
int SCascii = startChar;
int startInt = startingp[1];
if (!(ascii_A <= SCascii >= ascii_H) || !(1 <= startInt >= 8))
{
cout << "Your inputted move, " << startPos << ", is invalid." << endl;
cout << "Enter the coordinates of the piece you want to move. (eg A1) : ";
cin >> startingp;
cout << endl;
}
This condition:
(ascii_A <= SCascii >= ascii_H)
is not correct. Or at least, I don't think it's what you mean. Instead, do:
((ascii_A <= SCascii) && (SCascii >= ascii_H))
Other than that, your logic seems reasonable.
I made a Chess game as well, and this is what I used to get the coordinates:
Position* Interface::askPosition()
{
cout << "position: ";
string inp;
cin >> inp;
while (!(isalpha(inp[0]) && isdigit(inp[1]))) {
cout << "Please press CHAR + DIGIT\n";
cin >> inp;
}
return new Position(inp[0] - 'A', inp[1] - '1');
}
It's a static method of a class called Interface and returned a pointer to a Position object. However, this might be not a C++-clean way
You can just compare it with characters, no need to convert it into an int (ASCII).
char startChar = startingp[0];
int SCascii = startChar;
int startInt = startingp[1];
if (!(startChar>='A' && startChar<='H') || !(startInt>=1 && startInt<=8))
{
cout << "Your inputted move, " << startPos << ", is invalid." << endl;
cout << "Enter the coordinates of the piece you want to move. (eg A1) : ";
cin >> startingp;
cout << endl;
}

How can I clean this code up by using a loop?

Basically, this program allows a user to enter a sentence and depending on the users selection, it will show the middle character of the sentence, display it uppercase or lowercase, or backwards. Simple program, but I am new to programming so that may be the problem. I would like to figure out how to use loops instead of a ton of if statements. When I try to make some loops it breaks certain parts of the code but I am sure that is because I don't properly understand them. If you have any criticism or any advice on the code, I'd be happy to hear it. Thanks in advance!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sel;
string sent;
bool validinput;
int i;
int x;
int j;
int a;
cout << "Welcome to my program. Enter a sentence and select one of the options below.\n";
cout << "Enter -999 to exit the program." << endl;
cout << "============================================================================" << endl;
cout << endl;
cout << "1. Display the middle character if there is one." << endl;
cout << "2. Convert to uppercase." << endl;
cout << "3. Convert to lowercase." << endl;
cout << "4. Display backwards." << endl;
cout << "Enter a sentence: ";
getline (cin, sent);
cout << "Selection: ";
cin >> sel;
if (sel < 1 && sel > 4)
{
cout << "Invalid input. Try again. Selection: ";
cin >> sel;
validinput = false;
}
else (sel >= 1 && sel <= 4);
{
validinput = true;
}
if (validinput == true)
{
if (sel == 1)
{
j = sent.length() / 2;
cout << "The middle character is: " << sent.at(j) << endl;
}
if (sel == 2)
{
for (int i = 0; i < sent.length(); i++)
{
if (sent.at(i) >= 'a' && sent.at(i) <= 'z')
{
sent.at(i) = sent.at(i) - 'a' + 'A';
}
}
cout << "Uppercase: " << sent << endl;
}
if (sel == 3)
{
for (int x = 0; x < sent.length(); x++)
{
if (sent.at(x) >= 'A' && sent.at(x) <= 'Z')
{
sent.at(x) = sent.at(x) - 'A' + 'a';
}
}
cout << "Lowercase: " << sent << endl;
}
if (sel == 4)
{
for (a = sent.length() - 1; a >= 0; a--)
{
cout << sent.at(a);
}
}
}
system("pause");
return 0;
}
Personally I would use the switch selection statement. I roughly did this just to explain a bit on how it can make your code more friendly and understandable.
int sel;
bool validInput = false;
switch(sel)
{
case 1:
//display middle char if there's one
case 2:
//convert to uppercase
case 3:
//convert to lowercase
case 4:
//display backwards
validInput = true;
break;
default: //if number does not meat 1, 2, 3 or 4
validInput = false;
break;
}
As you may notice, for case 1, case 2, case 3 and case 4, there's a break just to say that if the number is between 1 to 4; validInput is true.
Reference: Switch Selection Statement
i suggest using a switch. It will organize your code better. From looking at your code you seem to have used for and if wisely. But I suggest the if statements checking for the input be replaced with switch.

Conditional cin giving stacked cout messages

Using C++ (g++-4.7 on Mint 16).
Code is a unrefined (and unfinished) Tic-Tac-Toe game.
#include <iostream>
using namespace std;
int main()
{
//initial data
char turn='A';
char ttt[] = {'1','2','3','4','5','6','7','8','9'};
int move;
int over=0; //0 is no, 1 is yes
int valid=0;
while ( over == 0)
{
//display
cout << "\n" << ttt[0] << "|" << ttt[1] << "|" << ttt[2] <<"\n-----\n";
cout << ttt[3] << "|" << ttt[4] << "|" << ttt[5] <<"\n-----\n";
cout << ttt[6] << "|" << ttt[7] << "|" << ttt[8] <<"\n\n Choose a number (Player " << turn << "):";
//ask enter for play with turn
cin >> move;
cout << "\n";
valid = 0;
while (valid == 0)
{
//check if input is valid
if (((move > 0) and (move < 10)) and
((ttt[move-1] != 'A') and (ttt[move-1] != 'B')) and
(cin))
{
ttt[move-1] = turn;
valid=1;
}
else
{
cout << "Invalid slot. Choose a number (Player " << turn << "):";
cin >> move;
cout << "\n";
}
}
//check if done if no //change turn then goto //display
if (((ttt[0]==ttt[1]) and (ttt[1]==ttt[2])) or
((ttt[3]==ttt[4]) and (ttt[4]==ttt[5])) or
((ttt[6]==ttt[7]) and (ttt[7]==ttt[8])) or
((ttt[0]==ttt[3]) and (ttt[3]==ttt[6])) or
((ttt[1]==ttt[4]) and (ttt[4]==ttt[7])) or
((ttt[2]==ttt[5]) and (ttt[5]==ttt[8])) or
((ttt[0]==ttt[4]) and (ttt[4]==ttt[8]))or
((ttt[2]==ttt[4]) and (ttt[4]==ttt[6])))
{
//display winner or say draw
cout << "Player " << turn << " wins!\n";
over=1;
}
else
{
//change turn
if (turn=='A')
{ turn='B';
}
else
{ turn='A';
}
}
}
return 0;
}
There seem to be a bug on the code. On the part where check if input is valid the and (cin) seem to be failing.
When entering a character, (Instead of a number) it output continuously stacks of:
Invalid slot. Choose a number (Player A or B):
I tested the rest of condition without it, it was all working well. Is there a problem on the code or is this really "cin" problem? I've also tried out !(!cin) but it's the same scenario.
You must clear the fail bit from the cin stream in your else block.
When you enter a character that isn't an integer, the cin stream sets the fail bit, which you correctly check for in your if statement, but you never clear it afterward. This causes your input validity check to be false forever.
#include <limits>
...
else
{
cin.clear(); // Add this line
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // And this one
cout << "Invalid slot. Choose a number (Player " << turn << "):";
cin >> move;
cout << "\n";
}
For additional information, see the documentation for std::basic_ios::clear
Update: see this question and this question for similar problems.
Essentially, you also need to tell cin to ignore whatever is in the stream or it will continually set the fail bit with its bad contents you haven't cleared yet. I modified the above snippet to work.

C++ string array & for loop doesnt work as i expected

#include <iostream>
using namespace std;
int main()
{
char num[10];
int a;
cout << "Odd or Even"<< endl;
for(;;)
{
cout << "Enter Number:" ;
cin >> num;
cout << endl;
for(a=9;a>=0;a--)
{
if(num[a]!='\0 && num[a]!=' ')
break;
}
if(num[a]==1 || num[a]==3 || num[a]==5 || num[a]==7 || num[a]==9)
cout << "Odd" << endl;
else
cout << "Even" << endl;
}
}
I am a rookie of C++,and I wrote a program to discriminate if a number is even or odd,
but no matter what number I enter, it only outputs "Even".
So I added these to find out when did the loop breaks:
cout << a << endl;
cout << "\"" << num[a] << "\"" << endl;
Result:
Enter Number:11
9
" "
Even
the for loop beraks when num[9]=' '? Which will lead to else and always output "Even".
You are confused about the character '1' and the number 1. They are different.
Instead of
if(num[a]==1 || num[a]==3 || num[a]==5 || num[a]==7 || num[a]==9)
you need
if(num[a]=='1' || num[a]=='3' || num[a]=='5' || num[a]=='7' || num[a]=='9')
Update
There is one more problems that is probably tripping you up.
num is not initialized. Zero-initialize it. Remember 0 is not the same as the character '0'.
char num[10] = {0};
Move the initialization of num inside the for loop. That will eliminate the problem of data from a previous execution of the loop from affecting the current execution of the loop.
Here's a version that works for me.
#include <iostream>
using namespace std;
int main()
{
cout << "Odd or Even"<< endl;
for(;;)
{
char num[10] = {0};
int a;
cout << "Enter Number:" ;
cin >> num;
cout << endl;
for(a=9;a>=0;a--)
{
if(num[a]!='\0' && num[a]!=' ')
break;
}
cout << num[a] << endl;
if(num[a]=='1' || num[a]=='3' || num[a]=='5' || num[a]=='7' || num[a]=='9')
cout << "Odd" << endl;
else
cout << "Even" << endl;
}
}
PS
You can replace the line
if(num[a]!='\0' && num[a]!=' ')
by
if(isdigit(num[a]))
That makes more sense to me.
If you are doing this with c++ there are much easier ways! Consider the following:
while (!done) {
string inputline;
getline(cin, inputline); //Now we have a string with the users input!
stringstream ss; // stringstreams help us parse data in strings!
int num; // We have a number we want to put it into.
ss >> num; // We can use the string stream to parse this number.
// You can even add error checking!
// Now to check for odd even, what do we know about even numbers? divisable by 2!
if (num % 2 == 0) // No remainder from /2
cout << Even << '\n'
else
cout << Odd << '\n'
}
see how you go with that!
Warning Untested code
You did a mistake (typo) here in this line..
if(num[a]!='\0 && num[a]!=' ')
it should be
if(num[a]!='\0' && num[a]!=' ')

2 minor errors i just cant get my head round in C++!

I have got a few niggling errors that i just cant get my new noob like brain round! what am i doing wrong i get the errors:
C:\Users\George\Desktop\linear_equation_calc\main.cpp||In function 'int main(int, const char**)':|
C:\Users\George\Desktop\linear_equation_calc\main.cpp|101|error: 'calcparallelplugin' was not declared in this scope|
C:\Users\George\Desktop\linear_equation_calc\main.cpp|104|error: 'else' without a previous 'if'|
||=== Build finished: 2 errors, 0 warnings ===|
#include <iostream>
#include <string.h>
using namespace std;
// Function includes
// I try to keep them in the order they appear in the
// output below for organization purposes
#include "calc.m.xy12plugin.cpp"
#include "calc.b.xymplugin.cpp"
#include "calc.m.xybplugin.cpp"
#include "calc.point.xymplugin.cpp"
#include "calc.parallelplugin.cpp"
// The above one would be here, too
int main(int argc, const char* argv[]) {
int i;
i = 0;
cout << "Linear Equation Calculator" << endl << "Copyright (c) 2011 Patrick Devaney" << endl
<< "Licensed under the Apache License Version 2" << endl;
// This loop makes the code a bit messy,
// but it's worth it so the program doesn't
// crash if one enters random crap such as
// "zrgxvd" or "54336564358"
while(i < 1) {
cout << "Type:" << endl
<< "0 to calculate a slope (the M value) based on two points on a line" << endl
<< "1 to calculate the Y-intercept (the B value) based on two points and a slope" << endl
<< "2 to calculate the slope (the M value) based on the Y-intercept and X and Y" << endl <<
"plug-ins" << endl
<< "3 to find the next point up or down a line based on the slope (M) and X and Y"
<< endl << "plug-ins" << endl
<< "4 to find a point x positions down the line based on the slope (M) and X and Y"
<< endl << "plug-ins" << endl
<< "5 to find the equation of a parallel line in form y=mx+c"
<< endl << "plug-ins" << endl;
string selection;
cin >> selection;
if(selection == "0") {
mcalcxyplugin();
i++;
}
else if(selection == "1") {
calcbxymplugin();
i++;
}
else if(selection == "2") {
calcmxybplugin();
i++;
}
else if(selection == "3") {
calcpointxymplugin(1);
i++;
}
else if(selection == "4") {
int a;
cout << "How many points up/down the line do you want? (Positive number for points" << endl
<< "further up, negative for previous points" << endl;
cin >> a;
calcpointxymplugin(a);
i++;
}
else if(selection == "5");{
calcparallelplugin();
i++;
}
else {
i = 1;
}
// End of that loop below
}
return 0;
}
Well, the first one means that in main you call calcparallelplugin(), and that's the first the compiler has heard of this function. Perhaps it's spelled differently in your include file?
The second error happens because of this stray semicolon:
else if(selection == "5");{
^
|
That serves as the body of the last "if" and therefore terminates the chain of statements; the last "else" a few lines later is therefore unrelated to any previous "if".
Trailing semi-colon is causing the else error:
else if(selection == "5");{
the trailing semi-colon means the code is equivalent to:
else if(selection == "5") { }
{
calcparallelplugin();
i++;
}
else {
i = 1;
}
so the else does not have a previous if: remove the semi-colon.