Connecting program to dictionary - c++

I'm writing this program for the game "Scattegories" and I would like to connect some sort of dictionary (to check if the words exist) to the program.
If I were to do that how would I do it?
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
void ltr() //gives a random letter
{
srand(time(NULL)); //gives a differant pattern every time
char letter;
letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table()
{
int plr, ctr;
string lst[5][20]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cout << "How many players?";
cin >> plr;
cout << "How many catagories?";
cin >> ctr; //parameters for later
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
cout << "Player number " << x+1<<":";
timer(); //gives time to write the words. Optimaly it would run in the background while each player writes the words.
for (int i = 0; i<ctr; i++) //changing catagory
{
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<ctr; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
sleep_for(5s);
}
int points() //points gained per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act() //running the program
{
int Points;
ltr();
table();
Points = points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int Points;
cout << "Starting in five seconds\n";
sleep_for(5s);
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act(); //keeping score of the rounds
}
else
{
auto end = std::chrono::high_resolution_clock::now();
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
sleep_for(5s); //time to read the last text
return 0;
}
/*
To do list:
-Convert to arduino
-Make timer work in background of of table
-Check if words in the table (for differant players) are the same and give points accordingly
-Check if words are actual words (connect an online dictonary?)
-Make interface? (if possible and I have time to learn how)
-Think of what to do with Hardwear
-Comment rest of the code
-Make a point count for each player
-change "srand" placement
*/
Please keep in mind that I'm relatively new to programing (almost half a year), so please try to explain it as simple as you can, thanks.

Can be an interesting exercise to implement your own dictionary.
Use a large array of characters to store the words, sorted alphabetically, each null-terminated. In addition, keep an array of indexes, each pointing to the start of a word.
Then implement a dichotomic search for lookup. It will be fairly efficient because you will find a word after at most Log(N) string comparisons, each taking between 1 and L character comparisons, where L is the word length.
A solution based on hashing can be more efficient, but is a little harder to code.

Related

Using a thread to stop an action

I am working on a program for the game Scattegories. In the program there is a table that the player inputs the words while a timer thread is working in the background. I would that when the time is over, the player won't be able to input anymore and for his turn to end.
How can I make that happen?
The table and the timer:
void timer()
{
cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 120; i > 0; i--)
{
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table(int plr)
{
string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" };
string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up
t1.detach();
cout << "When the timer ends please enter '0' in the remaining catagories\n";
for (int i = 0; i<12; i++) //changing catagory
{
cout << ctr[i] << ": ";
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
}
Full code:
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
void ltr() //gives a random letter
{
char letter;
letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 120; i > 0; i--)
{
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table(int plr)
{
string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" };
string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up
t1.detach();
cout << "When the timer ends please enter '0' in the remaining catagories\n";
for (int i = 0; i<12; i++) //changing catagory
{
cout << ctr[i] << ": ";
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<12; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
sleep_for(5s);
}
int points() //points gained per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act() //running the program
{
int Players, Points[6];
cout << "How many people are playing? (Up to six players)";
cin >> Players;
ltr();
table(Players);
//Points = points();
cout << "You have earned " << Points << " this round\n\n";
return 1;
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
srand(time(NULL)); //gives a differant pattern of letters every time
int Points;
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act(); //keeping score of the rounds
}
else
{
auto end = std::chrono::high_resolution_clock::now();
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
sleep_for(5s); //time to read the last text
return 0;
}
/*
To do list:
-Make timer stop the table when time is up
-Check if words in the table (for differant players) are the same and give points accordingly
-Check if words are actual words (connect an online dictonary?)
-Make interface? (if possible and I have time to learn how)
-Comment rest of the code
*/
P.S
I'm tying to keep this code portable so I preffer sugestions that will keep it this way. But I would appreciate any suggestion.
I'm using windows 10 (but I'm writing console aplications) I have access to c++11 and c++14
A non-portable solution
bool getUserInput(string &result, Time deadline) // pick a timer, any timer
{
while(deadline > Time::now()) // assuming your timer has a now function
{
if(kbhit()) // THIS IS NOT PORTABLE. On windows it's called _kbhit()
{
char key = getch(); //read the key (also non-portable)
if(key == '\n' or key == '\r')
{
return true;
}
else
{
string += key;
}
}
}
return false;
}

Visual studio 2015 hitting breakpoint

When I run the code I get the messege Scattegories.exe has triggered a breakpoint.
It happens after I enter the 12th nuber into my array, but the array still has a "spot" for it.
There is also a thread so maybe it's that, but it seems to work fine.
So I don't know what it is, thank you.
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
void ltr() //gives a random letter
{
char letter;
letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 10; i > 0; i--)
{
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table(int plr)
{
string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "A object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" };
string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
std::thread t1(timer); //gives time to write the words. Optimaly it would run in the background while each player writes the words.
for (int i = 0; i<12; i++) //changing catagory
{
cout << ctr[i] << ": ";
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<12; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
sleep_for(5s);
}
int points() //points gained per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act() //running the program
{
int Players, Points[6];
cout << "How many people are playing? (Up to six players)";
cin >> Players;
ltr();
table(Players);
//Points = points();
cout << "You have earned " << Points << " this round\n\n";
return 1;
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
srand(time(NULL)); //gives a differant pattern of letters every time
int Points;
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act(); //keeping score of the rounds
}
else
{
auto end = std::chrono::high_resolution_clock::now();
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
sleep_for(5s); //time to read the last text
return 0;
}
/*
To do list:
-Convert to arduino
-Make timer work in background of of table
-Check if words in the table (for differant players) are the same and give points accordingly
-Check if words are actual words (connect an online dictonary?)
-Make interface? (if possible and I have time to learn how)
-Think of what to do with Hardwear
-Comment rest of the code
*/
std::thread::~thread says this :
If *this has an associated thread (joinable() == true), std::terminate() is called.
If you ever destroy a std::thread that is joinable, std::terminate() is called. Visual Studio kindly breaks on std::terminate() to allow you to inspect what went wrong. In void table(int plr) you create a thread with std::thread t1(timer); but you never join with it or detach from it.
To solve this, your timer method should be changed to return immediately when the player has finished his turn, and then the main thread should join with it.

_sleep() function not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I get the error:
'_sleep': This function or variable has been superceded by newer
library or operating system functionality. Consider using Sleep
instead. See online help for details.
Can you guys and/or girls tell me what library is causing this error?
Code:
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
using std::cout;
using std::cin;
using std::string;
using std::getline;
void ltr() //gives a random letter
{
srand(time(NULL)); //gives a differant pattern every time
char letter;
letter = rand() % 27 + 64; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n";
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
_sleep(1000);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table()
{
int plr, ctr;
string lst[5][20]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin >> plr >> ctr; //parameters for later
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
timer();
for (int i = 0; i<ctr; i++) //changing catagory
{
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<ctr; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
_sleep(5000);
}
int points() //points per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act()
{
int Points;
ltr();
table();
Points = points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}
int main()
{
int Points;
cout << "Starting in five seconds\n";
_sleep(5000);
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act();
}
else
{
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
_sleep(5000); //time to read the last text
return 0;
}
/* To do list:
*Convert to arduino
*Make timer work in background of of table
*Check if words in the table (for differant players) are the same and give points accordingly
*Check if words are actual words (connect an online dictonary?)
*Make interface? (if possible and I have time to learn how)
*Think of what to do with Hardwear
*Comment rest of the code
*/
For using Sleep() function you need
#include <windows.h>

Program not randomly generating results

This is a board game created using C++. In order to win, one player must be able to reach the end of the board three times. But the problem is that it is the the first player who is entered into the board game that is always winning. I have a function setup so the that the simulated dice uses a random number-generator. How can I fix this issue?
#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#define SIZE 20 //EDITED: SIZE is Defined at 20 for the number of squares wanted for the board game. Makes is easier for change
//NOTE TO SELF: This is the revamped version. "EDITED" marks addition/change from 'assignment2_fixed.cpp
/**********************
* FUNCTION PROTOTYPES *
***********************/
void displayRules();
void action();
void firstRoll();
void showState();
int spinDie();
bool play();
bool winner();
int square[SIZE]; //For each square on the board, SIZE is Defined
const int loseTurn=1, switchPlace=2, goAgain=3, moveBack=4; //CONSTANT! Labels each action location for each square
int *location, *lastsq, *losTurn; //EDITED: Declared for dynamic allocation for location on board, last square of board, and lost turn
string *names; //EDITED: This is to hold the names of the players
int numPlayers = 0; //Holds the number of players
int turn; //EDITED: First players turn
/************
* FUNCTIONS *
*************/
void displayRules(){ //Displays the rules to the player once the game starts
cout << "Welcome to 3X" << endl;
cout << "The first player to go around the board three times wins!" <<endl;
cout << "The player with the lowest number goes first" << endl;
cout << "The players take turns until one player wins" << endl;
cout << "The players must move forward the number of spaces the die displays" << endl;
cout << "If the player lands on a square with an action, the player must perform that action, otherwise the next player goes" <<endl;
cout << endl << endl;
}
void action(){ //EDITED: This lists actions for each square
square[2] = goAgain;
square[4] = switchPlace;
square[6] = goAgain;
square[8] = loseTurn;
square[11] = goAgain;
square[14] = moveBack;
square[16] = switchPlace;
square[18] = loseTurn;
}
void firstRoll(){ //EDITED: Initializes for the first die roll
numPlayers = spinDie();
for(int i=0; i<numPlayers; i++)
location[i];
for(int i=0; i<numPlayers; i++)
lastsq[i];
for(int i=0; i<numPlayers; i++)
losTurn[i];
}
void showState(){
for(int i=0; i<numPlayers; i++){
cout << "Player " << names[i] << " is at location " << location[i] << endl;
}
for(int i=0; i<numPlayers; i++){
cout << names[i] << " lands on last space " << lastsq[i] << " times " << endl;
}
}
int spinDie(){ //For the dice. Number is generated randomly based on time
int x = rand()%6 +1 ;
cout<< " a "<< x<< " was rolled"<<endl;
return x;
}
bool play(){ //EDITED: Play function through squares
srand(time(0));
while(true){
if(losTurn[turn]){
losTurn[turn] = 0;//EDITED: Finds if player lost turn based on the value of 'turn'
turn = (turn) % numPlayers;
continue;
}
cout << "Turn: "<< names[turn] << endl;
int spot = spinDie(); //EDITED: Players spot on the board based on random number generator
cout << "Number on die : " << spot << endl;
location[turn] += spot;
if(location[turn] >= SIZE-1){
location[turn] -= SIZE;
lastsq[turn]++;
cout << names[turn] << " passes End!"<<endl;
if(winner())
return true;
}
switch(square[location[turn]]){
case goAgain:
cout<<"Action: Go again.\n";
showState();
break;
case loseTurn:
cout<<"Action: Lose turn.\n";
losTurn[turn] += 1;
break;
case switchPlace:
cout<<"Action: Switch places\n";
break;
case moveBack:
cout<<"Action: Move back 2 places.\n";
location[turn] -= 2;
break;
default:
cout<<"Action: None\n";
}
showState();
turn = (turn) % numPlayers;
}
return false;
}
bool winner(){ //Finds the winner who hits the end square three times
if(lastsq[turn] == 3){
cout<<"\n"<<names[turn]<<" WIN!\n";//Prints name of player with their turn
return true;
}
return false;
}
/***************
* MAIN PROGRAM *
****************/
int main(){
srand(time(0)); //Makes time based number in die random
displayRules(); //Diplays the rules first
cout << "Please type in the number of players between 1-6 : " << endl;
cin >> numPlayers;
while ((numPlayers < 1) || (numPlayers > 6)) //EDITED: Guard against any values outside of 1-6
{
cout << "ERROR: Enter a value from 1 - 6: ";
cin >> numPlayers;
}
names = new string[numPlayers]; //EDITED: Dynamically allocates array of string to hold names of players
location = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold locations of players
losTurn = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold lost turn of players
lastsq = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold count of each time player reach end square
for(int i=0; i<numPlayers; i++){
cout << "Enter name of player " << i+1 << " " << endl; //EDITED: To get name of each player starting with Player 1 (i+1)
cin >> names[i]; //EDITED: Gets names of players into the dynamically allocated strings
}
while(true){//EDITED: Continues the loop
if(play())//EDITED: If the "Play" function is true
break;//EDITED: Breaks once the winner is found
}
return 0;
}
Although your question is vague, I can give you some help to start you off. First off I'm going to show your code for the random factor on the dice.
int spinDie(){ //For the dice. Number is generated randomly based on time
int x = rand()%6 +1 ;
cout<< " a "<< x<< " was rolled"<<endl;
return x;
This code makes it so that the compiler will generate ANY number within an integer range. Then you divide that random number by 6 and find the remainder with the remainder operator (%). The +1 will add to the remainder if it is a factor of 6 such as 36. Your code is fine. The problem is that it is based off of time. The first one on the board probably gets similar or the same numbers each time and the second one does too. Which would explain the first one always winning. I'm assuming the first one gets similar or the same rolls every time because of your lack of explanation of the SPECIFIC problem. Hope this helped a little lol.

How can I use 2D array in C++ using Visual Studio?

I'm new to this site and programming in C++ language this semester.
I have been really trying for 2 days and have asked classmates but they do not know either. A classmate said to use the 2D arrays but I don't know what that is and my professor has not gone over 2D arrays.
I am stuck and would really appreciate help.
The input file contains this:
Plain_Egg 1.45
Bacon_and_Egg 2.45
Muffin 0.99
French_Toast 1.99
Fruit_Basket 2.49
Cereal 0.69
Coffee 0.50
Tea 0.75
idk how to display all the "users" orders
Basically a receipt, like he orders this and how many, then ask "u want anything else?", then take the order number and how many again, THEN at the end give back a receipt that looks like this
bacon_and_eggs $2.45
Muffin $0.99
Coffee $0.50
Tax $0.20
Amount Due $4.14
Here is my code:
// Libraries
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
//structures
struct menuItemType {
string itemName;
double itemCost;
};
// Prototypes
void header();
void readData(menuItemType menu[]);
void Display(menuItemType menu[]);
int main() {
header();
menuItemType menu [8];
readData(menu);
Display(menu);
//system("pause");
return 0;
}
void header() {
char c= 61;
for (int i=0; i < 64; i++) {
cout << c;
}
cout << c << endl;
cout << endl;
cout << "Breakfast Menu" <<endl;
for (int i=0; i < 64; i++) {
cout << c;
}
cout << "" << c << endl;
cout << endl;
}
void readData(menuItemType item[]) {
int i=0;
ifstream in;
in.open("input.txt");
cout << fixed << setprecision(2);
while(!in.eof()) {
in >> item[i].itemName >> item[i].itemCost;
++i;
}
}
void Display(menuItemType item[]) {
int choice = 0, quantity = 0;
double total = 0.0, totalF = 0.0, tax = 0.0;
char exit = 'y';
int j = 1, z = 1, i = 1;
//the Menu
for (int i=0; i<8; i++){
cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
j++;
}
cout << endl;
while(exit == 'y' || exit == 'Y') {
cout << "Please Enter your Selection or 0 to Exit : ";
cin >> choice;
if(cin.fail()) {
cout << "*******Invalid selection*******" << endl;
cin.clear();
cin.ignore(1000,'\n');
} else if (choice==0) {break; }
else {
cout<< "Enter Quantity: ";
cin>> quantity;
if (quantity==0) { break;}
else {
choice--;
total += (quantity * item[choice].itemCost);
tax = (total * .05);
totalF = total + tax;
cout << endl;
}
cout << endl;
cout << "======================================================" << endl;
cout << item[choice].itemName << "\t\t" << item[choice].itemCost << endl;
cout << "======================================================" << endl;
cout << "Do you want to continue (Y/N): ";
cin >> exit;
}
}
}
First off, you don't need a two dimensional array for this! You already have a one dimensional array of a suitable structure, as far as I can tell: Something which stores the name of the object and its price. What is somewhat missing is how many objects are currently in the array and how much space it has. If you want to go with the content of the entire array, make sure that you objects are correctly initialized, e.g., that the names are empty (this happens automatically, actually) and that the prices are zero (this does not).
I'm not sure if it is a copy&paste errors but the headers are incorrectly included. The include directives should look something like this:
#include <iostream>
The actual loop reading the values doesn't really work: You always need to check that the input was successful after you tried to read! Also, using eof() for checking that the loop ends is wrong (I don't know where people pick this up from; any book recommending the use of eof() for checking input loops is only useful for burning). The loop should look something like this:
while (i < ArraySize && in >> item[i].itemName >> item[i].itemCost)
++i;
}
This also fixes the potential boundary overrun in case there is more input than the array can consume. You might want to consider using a std::vector<Item> instead: this class keeps track of how many elements there are and you can append new elements as needed.
Note that you didn't quite say what you are stuck with: You'd need to come up with a clearer description of what your actual problem is. The above is just correcting existing errors and readjusting the direction to look into (i.e., forget about two dimensional arrays for now).