I'm a beginner at C++ and function,
Been getting the error "linker command failed with exit code 1" wondering anyone could give me some insight on how to resolve this problem, and on my overall code organization. Thank you very much for your time and opinion on this matter :)
ignore the below statement
(Due to lack of space I had to remove the #include statements which included the following iostream, iomanip, string and cmath).
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
void header();
double number();
int menu();
void seeMenu();
void processChoice();
void negPos();
int squareRoot();
void evenOdd();
void numOfDigits();
void digitAtPos();
using namespace std;
int main()
{
int choice = 0;
header();
number();
menu();
seeMenu();
processChoice();
while (choice != 0) {
processChoice();}
return 0;
}
void header(){
cout << "Number Manipulator"<< endl; }
double number() {
double num;
cout << "Enter a number to continue: ";
cin >> num;
return num; }
int menu(){
cout << "\nHere are your choices:";
cout << endl << setw(29) << "1) Is it even or odd?";
cout << endl << setw(38) << "2) Is it positive or negative?";
cout << endl << setw(49) << "3) What is the square root?";
cout << endl << setw(41) << "4) How many digits in the number?";
cout << endl << setw(54) << "5) What is the digit at a particular location?";
cout << endl << setw(19) << "0) To Quit.";
}
int showMenu() {
int choice;
cout << endl << endl <<"Enter your choice: ";
cin >> choice;
return choice; }
void processChoice(int choice) {
switch (choice) {
case 1: squareRoot();
break;
case 2: evenOdd();
break;
case 3: negPos();
break;
case 4: numOfDigits();
break;
case 5: digitAtPos();
break;
default:
cout << "This is not a valid choice. Please try again!";
showMenu();
break; }}
void negPos(int num) {
if (num > 0) cout << "Number is Positive"<< endl;
if (num < 0) cout << "Number is Negative"<< endl;
if (num == 0) cout << "Number is Zero"<< endl; }
void evenOdd(int num) {
if (num%2 == 0) cout << "Number is even";
else cout << "Number is odd"; }
void squareRoot(int num) {
int numSqrt;
numSqrt=sqrt(num);
cout << "Square root of " << num << " is " << numSqrt; }
void numOfDigits(int num) {
int numDigits=0;
do {
num /= 10;
numDigits++;
} while(num);
cout << "The number of digits in " << num << " is " << numDigits; }
void digitAtPos(int num) {
int pos;
cout << "What Position?: ";
cin >> pos; }
//need to be completed
While quickly looking over your code, I noticed that the function processMenuChoice is declared as void processMenuChoice(), but it is defined as void processMenuChoice(int choice). And since the function needs that paramater, you need to change your while loop to
processMenuChoice(choice);
while(choice != 0)
{
processMenuChoice(choice);
}
Here is the fix to make your code work. Note, there are some other issues that you need to fix.
Hint: in void squareRoot(int num), the result is an integer...
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
void displayHeader();
double getNumber();
int showMenu();
void menu();
void processMenuChoice(int choice, int num);
void isPosNeg(int num);
void squareRoot(int num);
void isOddEven(int num);
void findNumDigits(int num);
void findDigitAtPosition(int num);
using namespace std;
int main()
{
int choice = 0;
displayHeader();
int num = getNumber();
menu();
choice = showMenu();
while (choice != 0) {
processMenuChoice(choice, num);
choice = showMenu();
}
return 0;
}
void displayHeader(){
cout << "Number Manipulator\n"; }
double getNumber() {
double num;
cout << "Enter a number to continue: ";
cin >> num;
return num; }
void menu(){
cout << "\nHere are your choices:";
cout << endl << setw(29) << "1) Is it even or odd?";
cout << endl << setw(38) << "2) Is it positive or negative?";
cout << endl << setw(49) << "3) What is the square root?";
cout << endl << setw(41) << "4) How many digits in the number?";
cout << endl << setw(54) << "5) What is the digit at a particular location?";
cout << endl << setw(19) << "0) To Quit.";
}
int showMenu() {
int choice;
cout << "\n\nEnter your choice: ";
cin >> choice;
return choice; }
void processMenuChoice(int choice, int num) {
switch (choice) {
case 1: isPosNeg(num);
break;
case 2: isOddEven(num);
break;
case 3: squareRoot(num);
break;
case 4: findNumDigits(num);
break;
case 5: findDigitAtPosition(num);
break;
default:
cout << "This is not a valid choice. Please try again!";
showMenu();
break; }}
void isPosNeg(int num) {
if (num > 0) cout << "Number is Positive\n";
if (num < 0) cout << "Number is Negative\n";
if (num == 0) cout << "Number is Zero\n"; }
void isOddEven(int num) {
if (num%2 == 0) cout << "Number is even";
else cout << "Number is odd"; }
void squareRoot(int num) {
int numSqrt;
numSqrt=sqrt(num);
cout << "Square root of " << num << " is " << numSqrt; }
void findNumDigits(int num) {
int numDigits=0;
do {
num /= 10;
numDigits++;
} while(num);
cout << "The number of digits in " << num << " is " << numDigits; }
void findDigitAtPosition(int num) {
int pos;
cout << "What Position?: ";
cin >> pos; }
//need to be completed
Related
I have an assignment here that I've been working on for the past few hours and have been met with a problem. Whenever I compile and run the program in VS Code it throws me into an infinite loop where the text of the "menu" is printed repeatedly. I imagine this has something to do with the for(;;){ loop at the beginning of the menu.
I've deleted the for(;;){ statement at the beginning of the menu, and the continuous scrolling stopped, but I was unable to input any numbers (cases) and the program, essentially, just printed the menu and that was the end.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student {
string name;
float GPA;
public:
string getName() const
{
return name;
}
float getGPA() const
{
return GPA;
}
void setName(string Name)
{
name = Name;
}
void setGPA(float gpa)
{
GPA = gpa;
}
Student(const string& name, float gpa)
: name(name)
, GPA(gpa)
{
}
Student()
{
}
void printDetails(Student s[], int n)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n; i++) {
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
float calcAverageGPA(Student s[], int n)
{
float avg = 0;
float sum = 0;
for (int i = 0; i <= n; i++) {
sum += s[i].getGPA();
}
avg = sum / n;
return avg;
}
float getGPAbyName(Student s[], int n, string name)
{
for (int i = 0; i <= n - 1; i++) {
if (s[i].getName() == name)
return s[i].getGPA();
}
return -1;
}
void showListByGPA(Student s[], int n, float gpa)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n - 1; i++) {
if (s[i].getGPA() > gpa)
;
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
};
int main()
{
int n = 0;
int ch;
Student s[50];
string name;
float GPA;
float avg = 0;
cout << "======================" << endl;
cout << "(1): Add a student" << endl;
cout << "(2): Print the details of a student" << endl;
cout << "(3): Get the GPA of a Student by name." << endl;
cout << "(4): Get names of students based on GPA." << endl;
cout << "(6): Quit the program." << endl;
cout << "Enter your option" << endl;
switch (ch) {
case '1':
cout << "\nEnter student's name" << endl;
cin >> name;
cout << "Enter GPA" << endl;
cin >> GPA;
s[n] = Student(name, GPA);
n++;
break;
case '2':
s[0].printDetails(s, n);
break;
case '3':
cout << "\nEnter the name of a student" << endl;
cin >> name;
GPA = s[0].getGPAbyName(s, n, name);
if (GPA == -1) {
cout << "\nStudent not found!" << endl;
}
else
cout << "\nGPA is: " << endl;
break;
case '4':
cout << "\nEnter GPA: " << endl;
cin >> GPA;
s[0].showListByGPA(s, n, GPA);
break;
case '5':
avg = s[0].calcAverageGPA(s, n);
cout << "\nAverage GPA: " << avg << endl;
break;
case '6':
exit(0);
}
}
I suspect the problem resides in main(). I included the prior blocks of the program in case they were necessary to provide any suggestions.
You obviously want to read in "ch".
You've also made this an int, and don't zero-initialize, which can cause some undefined behaviour if you don't set it beforehand.
The switch case should be
switch(X){
case 1:
// Code
break;
}
etc..
The difference is "1" or 1. It evaluates an enumeration value, instead of strings.
For safety: you might want to add a case default, which is common practice.
Hello once again stack overflow users! I have a new program, and have once again ran into a bit of a problem I cannot figure out! I wrote a program, math tutor program, which is practically finished, just that there are a few things I cannot figure out. In the program, there is a void function that checks the answers (will display if user input is correct or incorrect) but I cant seem to get it to work. When I have it in my doOneSet void function (does exactly one set or problems) it seems to only display "incorrect" even though the answer is correct? I cant seem to figure out what I did wrong or what is missing. Any type of help/tips/references is appreciated. Thank you!
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void getProbsPerSet (int& numProbs);
void printHeader (/*in*/ char problemType);
void getMaxNum(/* out */int& maxNum);
void generateOperands(int& num1, int& num2, int maxNum);
void checkAnswer (/* in */int num1,/* in */ int num2, /*out*/ int& answer);
void calcCorrectAnswer(/* in */ char problemType,/* in */ int num1,/* in */int num2, /*inout*/ int& answer);
void doOneProblem (char problemType, int maxNum);
void doOneSet (char problemType, int probsPerSet, int&);
void printReport (/* in */ int probsPerSet, int& set1Correct, /* in */int& set2Correct, /* in */int& set3Correct);
int main ()
{
int set1Correct, set2Correct, set3Correct, probsPerSet, maxNum;
srand(time(0));
getProbsPerSet (probsPerSet);
cout << endl;
doOneSet ('+', probsPerSet, set1Correct);
cout << endl;
doOneSet ('-', probsPerSet, set2Correct);
cout << endl;
doOneSet ('*', probsPerSet, set3Correct);
cout << endl;
return 0;
}
void getProbsPerSet (int& numProbs)
{
cout << "Enter problems per set: ";
cin >> numProbs;
cout << endl;
while (numProbs < 3 || numProbs > 10)
{
cout << endl;
cout << "Please stay between 3 and 10. Thank you!";
cout << endl;
cout << endl;
cout << "Enter problems per set: ";
cin >> numProbs;
cout << endl;
}
}
void printHeader (/*in*/ char problemType)
{
switch(problemType)
{
case '+': cout << endl;
cout << "Set # 1" << endl;
cout << "----------" << endl;
break;
case '-': cout << endl;
cout << "Set # 2" << endl;
cout << "----------" << endl;
break;
case '*': cout << endl;
cout << "Set # 3" << endl;
cout << " ----------" << endl;
break;
}
}
void doOneProblem (char problemType, int maxNum)
{
int num1,num2,answer;
generateOperands(num1, num2, maxNum);
switch (problemType)
{
case '+' : cout << num1 << problemType << num2 << " = ";
cin >> answer;
break;
case '-' : cout << num1 << problemType << num2 << " = ";
cin >> answer;
break;
case '*' : cout << num1 << problemType << num2 << " = ";
cin >> answer;
break;
}
}
void doOneSet (char problemType, int probsPerSet, int& answer)
{
int num1, num2, numProbs, maxNum;
bool isCorrect;
printHeader(problemType);
getMaxNum(maxNum);
for (int count = 0; count < probsPerSet; count++)
{
generateOperands(num1, num2, maxNum);
doOneProblem (problemType, maxNum);
calcCorrectAnswer(problemType, num1, num2, answer);
checkAnswer (num1, num2, answer);
}
}
void generateOperands(int& num1, int& num2, int maxNum)
{
num1 = 1 + rand() % maxNum;
num2 = 1 + rand() % maxNum;
}
void getMaxNum(/*out*/ int& maxNum)
{
cout << "What is the maximum number for this set?: ";
cin >> maxNum;
}
void checkAnswer (int num1, int num2, /*out*/ int& answer)
{
bool isCorrect;
if (answer == isCorrect)
{
cout << endl;
cout << "Correct!" << endl;
cout << endl;
}
else
{
cout << endl;
cout << "Incorrect!" << endl;
cout << endl;
}
}
void calcCorrectAnswer(/* in */ char problemType,/* in */ int num1,/* in */int num2, /*inout*/ int& answer)
{
bool isCorrect;
switch (problemType)
{
case '+' : isCorrect = num1 + num2;
break;
case '-' : isCorrect = num1 - num2;
break;
case '*' : isCorrect = num1 * num2;
break;
}
}
void printReport (/* in */ int probsPerSet, int& set1Correct, /*in*/ int& set2Correct, /*in*/ int& set3Correct)
{
int set1Percent = 0, set2Percent = 0, set3Percent = 0;
int total, complete;
int numcorrect = 1;
total = set1Correct + set2Correct + set3Correct;
complete = probsPerSet + probsPerSet + probsPerSet;
set1Percent = (100 * set1Correct) / probsPerSet;
set2Percent = (100 * set2Correct) / probsPerSet;
set3Percent = (100 * set3Correct) / probsPerSet;
cout << endl;
cout << "Set #1 : You got " << set1Correct << " correct out of " << probsPerSet << " for " << set1Percent << "%" << endl;
cout << "Set #2 : You got " << set2Correct << " correct out of " << probsPerSet << " for " << set2Percent << "%" << endl;
cout << "Set #3 : You got " << set3Correct << " correct out of " << probsPerSet << " for " << set3Percent << "%" << endl;
cout << endl;
cout << "Overall you got " << total << " out of " << complete << endl;;
}
The problem is your bool isCorrect;. Both of them. 😎
So, in one function, calcCorrectAnswer, you declare this variable, then set it according to the logic of your requirements. Fine.
Then in another function, checkAnswer, you declare it again, then compare it to true/false to choose which output to produce.
But these are different variables. Despite sharing a name, they are scoped to the function they're in, so setting one has no effect on the other. The one in checkAnswer is uninitialised and never takes a value, so your program has undefined behaviour.
You could return your boolean from calcCorrectAnswer and pass it as an argument to checkAnswer. Or you could just merge those two functions; there doesn't seem to be a big reason to keep them separate.
In the adding user section in the code below, I am unable to type any characters for the "Add another person?(y/n): " question. it just jumps back to entering age. How do I fix this?
I've tried to change ans into a string, implement a while loop to force the question to show up, and many other things. It just seems that nothing works and I've been trying it for the good part of two hours
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
char ans;
int people;
int option;
int count = 0;
struct data
{
string name;
int age;
char gender;
string comments;
}person[100];
// homescreen
homescreen:
cout << "Welcome to the Data Base!" << endl;
cout << endl;
// displaying all people
for (int list = 0; list < count; list++)
{
cout << list << ".) " << person[list].name << endl;
}
cout << endl;
cout << "[1] View Person" << endl;
cout << "[2] Add Person" << endl;
cout << "[3] Edit Person" << endl;
cout << "[4] Delete Person" << endl;
cout << "[5] Exit" << endl;
cout << "Choose Option: "; cin >> option;
// using options
while (option != 5)
{
if (option == 1)
{
view:
for (int list2 = 0; list2 < count; list2++)
{
cout << list2 << ".) " << person[list2].name << endl;
}
cout << endl;
cout << "Enter number of person you want: "; cin >> people;
system("cls");
cout << "Name: " << person[count].name << endl;
cout << "Age: " << person[count].age << endl;
cout << "Gender: " << person[count].gender << endl;
cout << "Comments: " << person[count].comments << endl << endl;
cout << "View another person?(y/n): "; cin >> ans;
if (ans == 'y')
{
system("cls"); goto view;
}
else if (ans == 'n')
{
system("cls"); goto homescreen;
}
}
if (option == 2)
{
add:
system("cls");
cout << "Name: "; cin >> person[count].name;
system("cls");
cout << "Age: "; cin >> person[count].age;
system("cls");
cout << "Gender(M/F/H): "; cin >> person[count].gender;
system("cls");
cout << "Comments: "; cin >> person[count].comments;
count++;
system("cls");
cout << "Add another person?(y/n): "; cin >> ans;
if (ans == 'y')
{
system("cls");
goto add;
}
else if (ans == 'n')
{
system("cls");
goto homescreen;
}
}
}
}
If you anybody can help me I'd be grateful
The goto statements in your code makes the program really good
spaghetti
structure and that is not
good.
Therefore, think instead of goto other options, such as infinite
while loop which will break once the user enters the n or moving
the code to the function.
Secondly what if you have not entered any persons and choosing the
option 1. You still output the attributes of the person as
count is initialized zero at least. Remember the attributes are
not initialized at this point. Accessing the uninitialized
variables will invoke undefined
behavior. Therefore,
provide a check (something like if(count > 0) )before you execute the code in option 1.
In addition to that, remember that
std::endl flushes the output buffer, and '\n' doesn't. Therefore, most of
the cases you might wanna use just
\n.
Last but not the least, use std::vector instead of the using C style arrays with some predefined size. What if the user has more than 100 inputs? The solution in C++ is std::vector, which can expand dynamically as its storage is handled automatically.
Following is a possible solution to your program, in which the comments will guide you through to the things that I mentioned above.
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
struct Data
{
std::string name;
int age;
char gender;
std::string comments;
Data(const std::string& n, int a, char g, const std::string& c) // provide a Constructor
:name(n), age(a), gender(g), comments(c)
{}
};
void debugMsg(const std::string& msg)
{
system("cls");
std::cout << "\n\n\t\t" << msg << "\n\n";
Sleep(3000);
}
int main()
{
std::vector<Data> person; // use std::vector to store the datas
while (true) // loop: 1
{
system("cls");
std::cout << "Welcome to the Data Base! \n\n";
std::cout << "[1] View Person\n";
std::cout << "[2] Add Person\n";
std::cout << "[3] Edit Person\n";
std::cout << "[4] Delete Person\n";
std::cout << "[5] Exit\n";
std::cout << "Choose Option: ";
int option; std::cin >> option;
switch (option) // use switch to validate the options
{
case 1:
{
while (true) // loop - 2 -> case 1
{
// if no data available to show -> just break the loop 2 and return to the outer loop(i.e, loop 1)
if (person.empty()) { debugMsg("No person available to show ....going to main manu...."); break; }
// otherwise: displaying all people
for (std::size_t index = 0; index < person.size(); ++index)
std::cout << index << ".) " << person[index].name << "\n";
std::cout << "\nEnter number of person you want: ";
std::size_t index; std::cin >> index;
// if the index is not valid -> just break the loop 2 and return to the outer loop(i.e, loop 1)
if (index < 0 || index >= person.size()) { debugMsg("Sorry, wrong index!... returning to the main menu......"); break; }
system("cls");
std::cout << "Name: " << person[index].name << std::endl;
std::cout << "Age: " << person[index].age << std::endl;
std::cout << "Gender: " << person[index].gender << std::endl;
std::cout << "Comments: " << person[index].comments << std::endl << std::endl;
std::cout << "View another person?(y/n): ";
char ans; std::cin >> ans;
if (ans == 'y') { system("cls"); continue; } // just continue looping
else if (ans == 'n') { break; } // this will break the loop 2 and return to the outer loop(i.e, loop 1)
else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
}
} break;
case 2:
{
while (true) // loop - 3 -> case 2
{
system("cls");
std::string name, comments; int age; char gender;
std::cout << "Name: "; std::cin >> name;
std::cout << "Age: "; std::cin >> age;
std::cout << "Gender(M/F/H): "; std::cin >> gender;
std::cout << "Comments: "; std::cin >> comments;
// simply construct the Data in person vector in place
person.emplace_back(name, age, gender, comments);
std::cout << "\n\nAdd another person?(y/n): ";
char ans; std::cin >> ans;
if (ans == 'y') { system("cls"); continue; }
else if (ans == 'n') { system("cls"); break; } // same as case 1
else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
}
} break;
case 3: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
case 4: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
case 5: return 0; // if its 5, just retun the main
default: break;
}
}
return 0;
}
As mentioned above, using "goto" is a bad style, so i would suggest structure your program a little. Below is my version.
Naturally, I did not add any checks and controls, the author will be able to do this on his own. But main logics should work. And, of course, it is better to use vector instead of static array.
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
enum options { OPT_VIEW = 1, OPT_ADD = 2, OPT_EDIT = 3, OPT_DELETE = 4, OPT_EXIT = 5 };
struct data
{
string name;
int age;
char gender;
string comments;
};
class App
{
private:
data person[100];
int count = 0;
public:
App();
void Run();
int HomeScreen();
void View();
void Add();
};
App::App() : count(0)
{}
void App::Run()
{
int option = HomeScreen();
while(option != OPT_EXIT)
{
switch(option)
{
case OPT_VIEW:
View();
break;
case OPT_ADD:
Add();
break;
}
option = HomeScreen();
}
}
int App::HomeScreen()
{
int option = 0;
cout << "Welcome to the Data Base!" << endl;
cout << endl;
// displaying all people
for(int list = 0; list < count; list++)
{
cout << list << ".) " << person[list].name << endl;
}
cout << endl;
cout << "[1] View Person" << endl;
cout << "[2] Add Person" << endl;
cout << "[3] Edit Person" << endl;
cout << "[4] Delete Person" << endl;
cout << "[5] Exit" << endl;
cout << "Choose Option: "; cin >> option;
return option;
}
void App::View()
{
char ans = 0;
do
{
int people = 0;
for(int list2 = 0; list2 < count; list2++)
{
cout << list2 << ".) " << person[list2].name << endl;
}
cout << endl;
cout << "Enter number of person you want: "; cin >> people;
system("cls");
cout << "Name: " << person[people].name << endl;
cout << "Age: " << person[people].age << endl;
cout << "Gender: " << person[people].gender << endl;
cout << "Comments: " << person[people].comments << endl << endl;
cout << "View another person?(y/n): "; cin >> ans;
}
while(ans == 'y');
system("cls");
}
void App::Add()
{
char ans = 0;
do
{
system("cls");
cout << "Name: "; cin >> person[count].name;
system("cls");
cout << "Age: "; cin >> person[count].age;
system("cls");
cout << "Gender(M/F/H): "; cin >> person[count].gender;
system("cls");
cout << "Comments: "; cin >> person[count].comments;
count++;
system("cls");
cout << "Add another person?(y/n): "; cin >> ans;
}
while(ans == 'y');
system("cls");
}
int main()
{
App program;
program.Run();
}
I am a beginner taking a programming class and I am having trouble with my assignment. I am to make a program that can store and sort data(I chose games), and everything seems to be going alright. EXCEPT for when I choose to input a game, and later display the games I've entered, there will be nothing in the list. Is there something I'm missing?
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct VidyaGames {
string Title;
string Date;
string Developer;
};
void getGames(VidyaGames array[], int &k);
void displayGames(VidyaGames array[], int &k);
void deleteGames(VidyaGames array[], int &k);
void sortGames(VidyaGames array[], int &k);
const int MAX = 150;
string Title;
string Date;
string Developer;
int main()
{
char choice;
VidyaGames array[MAX];
bool kek = true;
int k = 0;
do
{
cout << "Welcome to the 'Super Incredible Amazing Game Storage-o-Tron 5000(and one)." << endl;
cout << " " << endl;
cout << "Please select which task you would like to perform by typing in the " << endl;
cout << "corresponding letter in the bracket: " << endl;
cout << " " << endl;
cout << "[I]nput a game into the list." << endl;
cout << "[D]isplay the games you have stored." << endl;
cout << "[S]ort the games you have stored." << endl;
cout << "[R]emove a game from the list." << endl;
cout << "[Q]uit the program." << endl;
cin >> choice;
switch (choice)
{
case 'I': getGames(array, k); break;
case 'D': displayGames(array, k); break;
case 'S': deleteGames(array, k); break;
case 'R': deleteGames(array, k); break;
case 'Q': kek = false; break;
default : cout << "Hey. Remember when I gave you the specific options you were allowed to choose?" << endl;
cout << "Maybe enter one of those?" << endl;
cout << " " << endl;
}
}
while (kek);
cout << "You have killed me." << endl;
}
void getGames(VidyaGames array[], int &k)
{
system("cls");
VidyaGames tmp;
char lel[100];
cout << "Enter the title of your game: " << endl;
getline (cin, Title);
cout << "Enter the date your game was published: (Example: March 15th, 2014)" << endl;
getline (cin, Date);
cout << "Enter the developer of your game: " << endl;
getline (cin, Developer);
}
void displayGames(VidyaGames array[], int &k)
{
system ("cls");
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else if (k > 0) {
for (int i=0; i < 0; i++)
{
cout << "Title: " << array[i].Title << endl;
cout << "Release Date: " << array[i].Date << endl;
cout << "Developer: " << array[i].Developer << endl;
}
}
}
void deleteGames(VidyaGames array[], int &k) {
system("cls");
char deleteChoice;
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else {
cout << "Please type the name of the game you would like to delete: " << endl;
cin >> deleteChoice;
}
}
void sortGames(VidyaGames array[], int &k)
{
}
you forget to set value in your array.
i add 4 lines to function getGames:
array->Title = Title;
array->Date = Date;
array->Developer = Developer;
k++;
and change one line in function displayGames:
for (int i=0; i < k; i++)
this is final code:
// test_3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct VidyaGames {
string Title;
string Date;
string Developer;
};
void getGames(VidyaGames array[], int &k);
void displayGames(VidyaGames array[], int &k);
void deleteGames(VidyaGames array[], int &k);
void sortGames(VidyaGames array[], int &k);
const int MAX = 150;
string Title;
string Date;
string Developer;
int main()
{
char choice;
VidyaGames array[MAX];
bool kek = true;
int k = 0;
do
{
cout << "Welcome to the 'Super Incredible Amazing Game Storage-o-Tron 5000(and one)." << endl;
cout << " " << endl;
cout << "Please select which task you would like to perform by typing in the " << endl;
cout << "corresponding letter in the bracket: " << endl;
cout << " " << endl;
cout << "[I]nput a game into the list." << endl;
cout << "[D]isplay the games you have stored." << endl;
cout << "[S]ort the games you have stored." << endl;
cout << "[R]emove a game from the list." << endl;
cout << "[Q]uit the program." << endl;
cin >> choice;
switch (choice)
{
case 'I': getGames(array, k); break;
case 'D': displayGames(array, k); break;
case 'S': deleteGames(array, k); break;
case 'R': deleteGames(array, k); break;
case 'Q': kek = false; break;
default : cout << "Hey. Remember when I gave you the specific options you were allowed to choose?" << endl;
cout << "Maybe enter one of those?" << endl;
cout << " " << endl;
}
}
while (kek);
cout << "You have killed me." << endl;
}
void getGames(VidyaGames array[], int &k)
{
system("cls");
VidyaGames tmp;
char lel[100];
cout << "Enter the title of your game: " << endl;
getline (cin, Title);
cout << "Enter the date your game was published: (Example: March 15th, 2014)" << endl;
getline (cin, Date);
cout << "Enter the developer of your game: " << endl;
getline (cin, Developer);
array->Title = Title;
array->Date = Date;
array->Developer = Developer;
k++;
}
void displayGames(VidyaGames array[], int &k)
{
system ("cls");
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else if (k > 0) {
for (int i=0; i < k; i++)
{
cout << "Title: " << array[i].Title << endl;
cout << "Release Date: " << array[i].Date << endl;
cout << "Developer: " << array[i].Developer << endl;
}
}
}
void deleteGames(VidyaGames array[], int &k) {
system("cls");
char deleteChoice;
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else {
cout << "Please type the name of the game you would like to delete: " << endl;
cin >> deleteChoice;
}
}
void sortGames(VidyaGames array[], int &k)
{
}
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;
while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}
cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;
while (letter != 'N' && letter != 'n')
{
while (wrong_guesses < MAX_TRIES)
{
cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}
cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}
cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}
int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}
void manual()
{
string word;
cout << endl << "INPUT WORD: " << endl;
cin >> word;
return;
}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;
srand(time(0));
word = rand() % 20;
while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}
do
{
fin >> x;
roc[i++] = char(x);
} while (x);
return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;
for (i = 0; i<MAX_LENGTH; i++)
{
if (secretword[i] == 0)
{
break;
}
if (guess == guessword[i])
{
return 0;
}
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);
for (i = 0; i<length; i++)
{
unknown[i] = '*';
}
unknown[i] = 0;
}
Again
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.