#include <iostream>
using namespace std;
int main() {
char gun;
char att;
cout << "Guns" << endl;
cout << "[1] AK" << endl;
cout << "[2] MP5" << endl;
cout << "[3] M2" << endl;
cout << "[4] SAR" << endl;
cout << "[5] Tommy" << endl;
cout << "[6] Custom" << endl;
cin >> gun;
cout << "[A] Holosight" << endl;
cout << "[B] Simplesight" << endl;
cout << "[C] Silencer" << endl;
cout << "[D] Muzzel Boost" << endl;
cout << "[E] Muzzel Break" << endl;
cout << "[F] 8x Zoom Scope" << endl;
cout << "[G] 16x Zoom Scope" << endl;
cout << "[H] Lasersight" << endl;
cin >> att;
switch (gun) {
case'1':
if (gun = 1)
cout << 'test' << endl;
else
cout << "Wrong Command" << endl;
break;
}
}
When I run this and select 1 (code is only half-built) it prints "1952805748". I have tried adding more cases and it still has this problem.
Can someone please help me with this problem.
You have several issues here:
if (gun = 1)
cout << 'test' << endl;
that's not a comparison; gun is always assigned the value 1, and the if condition is always true. The comparison operator is ==.
Also, you are printing out 'test', which is why you are getting the weird number. You need to print "test".
Also note that gun is a char, so you might want to compare against the char '1'.
Also, avoid using namespace std;
Related
Good day everyone. My program is about a Computer Shop (Laptop). So, people can buy laptop through this program and "PCPurchase" is the function that gonna handle the customer transaction.
I use file-pointer and struct to save information about all the laptops. And of course, the struct store more than 1 type of laptops because obviously this is a laptop shop.
I encounter a problem where user can only buy (enter name) of the first entry (first laptop) in the struct. Here: cout << "Enter the laptop company and name you want to buy: " << endl;
If customer enter the name of 2nd laptop,3rd and so on, it will jump to line
cout << endl << "\tNot available!" << endl; cout << "\tPress A to try again or B to return to main menu" << endl;
It indicates that the laptop's name is not in database which actually is.
Can I know what the problem here actually is?
int PCPurchase()
{
struct customer cust;
system("color 0A");
char laptop[100];
double total_bill;
const double TAX=0.06;
system("cls");
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "\t\tCustomer Dashboard" << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
fptr=fopen("laptop.txt","ab+");
cout << "Available laptops: " << endl;
rewind(fptr);
while(fread(&PC,sizeof(PC),1,fptr)==1)
{
cout << endl << "Laptop company and name: ";
cout << PC.laptopcompany << endl;
cout << "RAM: ";
cout << PC.RAM << endl;
cout << "Processor: ";
cout << PC.Processor << endl;
cout << "Price: RM";
cout << PC.price << endl;
}
cout << "\nPress any key to continue purchase" << endl;
getch();
fflush(stdin);
getInfo(cust); //get information of customer
cout << "Enter the laptop company and name you want to buy: " << endl;
cout << "(Type 'RETURN' if you do not want to purchase)" << endl << endl;
gets(laptop);
rewind(fptr);
while(fread(&PC,sizeof(PC),1,fptr)==1)
{
if(strcmpi(PC.laptopcompany,laptop)==0)
{
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "\tYou have selected" << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "Laptop company and name: ";
cout << PC.laptopcompany << endl;
cout << "RAM: ";
cout << PC.RAM << endl;
cout << "Processor: ";
cout << PC.Processor << endl;
cout << "Price: ";
cout << PC.price << endl;
total_bill=PC.price+(PC.price*TAX);
cout << setfill ('-') << setw (55) << "-" << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Name: "<< cust.name << endl; // struct output
cout << "Email: "<< cust.email << endl;
cout << "Phone Number: " << cust.number << endl;
cout << "Your total bill (including 6% tax): RM" << total_bill << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
cout << endl << "\tPress 1 to return to main screen!";
cout << endl << "\tPress 2 to quit the program!";
char afterpurchase;
afterpurchase=getche();
if (afterpurchase=='1')
{
fclose(fptr);
main();
}
else
exit_system();
}
else if(strcmpi("RETURN",laptop)==0)
main();
else
{
cout << endl << "\tNot available!" << endl;
cout << "\tPress A to try again or B to return to main menu" << endl;
char choice1;
choice1=getche();
choice1=toupper(choice1); // Transform to uppercase
switch (choice1)
{
case 'A': fclose(fptr);
PCPurchase();
break;
default : fclose(fptr);
main();
break;
}
}
}
}
It is because of else statement. If you want to buy the laptop from the second record of the data and so on, it will compare with the first record and will not return true. So, it will proceed to else statement and will not repeat the while loop. Simply change the else statement to make the loop working.
I usually find answers for my problems on this forum, but this time I can't. This is why I am asking you. (Sorry in advance for the mistakes I'm doing in english or c++)
So here is my void function, it is just the start of a little game I'm making in the console. It is just showing some informations. By the way it's a french game, but I don't think that a traduction is needed to solve my problem.
void showIntro (Personnage a, Personnage b, Personnage c)
{
cout << endl;
cout << "VOUS ENTREZ DANS LES MINES DE LA MORIA" << endl << endl;
cout << "ENNEMIS PRESENTS : " << endl << endl;
cout << " " << "Orc robuste" << endl << endl << endl;
a.afficherEtatIntro ();
cout << endl << endl;
cout << " " << "Orc robuste" << endl << endl << endl;
b.afficherEtatIntro ();
cout << endl << endl;
cout << " " << "Orc robuste" << endl << endl << endl;
c.afficherEtatIntro ();
cout << endl << endl;
cout << "Le combat va commencer";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << "." << endl << endl << endl;
cout << "Gimli : ";
string phraseGimli ("Je vengerai mes freres!");
for (int f = 0; f <= phraseGimli.size(); f++) // Just writing a sentence
{
cout << phraseGimli[f];
Sleep(100);
}
cout << endl << "Legolas : ";
string phraseLegolas ("Nous sommes avec toi mon ami");
for (int g = 0; g <= phraseLegolas.size(); g++) // Writing a sentence
{
cout << phraseLegolas[g];
Sleep(100);
}
cout << endl << "Aragorn : ";
string phraseAragorn ("POUR LE GONDOR");
for (int i = 0; i <= phraseAragorn.size(); i++) // Writing a sentence
{
cout << phraseAragorn[i];
Sleep(100);
}
}
And here is the other one.
void choixAttaque (Personnage joueur, Personnage a, Personnage b, Personnage c, string nom)
{
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << "Qui souhaitez vous attaquer ?" << endl;
cout << endl;
cout << endl;
cout << " " << nom << endl << endl << endl;
a.afficherEtatIntro ();
cout << endl << "Tapez 1" << endl;
cout << " " << nom << endl << endl << endl;
b.afficherEtatIntro ();
cout << endl << "Tapez 2" << endl;
cout << " " << nom << endl << endl << endl;
c.afficherEtatIntro ();
cout << endl << "Tapez 3" << endl;
int choix;
do
{
cin >> choix;
} while (choix <= 0 || choix >= 4);
switch (choix)
{
case 1:
joueur.attaquer(a);
case 2:
joueur.attaquer(b);
case 3:
joueur.attaquer(c);
}
}
afficherEtatIntro ()
void Personnage::afficherEtatIntro () const
{
cout << " " << "Vie : " << m_vie << endl;
cout << " ";
m_arme->afficher();
}
m_arme means m_weapon, I used "->" because it is from a pointer. I'm new to all of this, so it can be really possible that the mistake comes from here.
When I'm using only one, it works, but when I'm using both in a row, it does that.
first void function showing properly
And then.
Path of things in my computer
I hope I gave the informations needed to solve the problem, and I hope it can be solved. Thanks in advance for everything, and sorry for all the mistakes I may have made in english, c++ or by linking my lines of code. ^^'
(Btw, all the "cout << endl;" looks ugly I guess, but that is just until I learn how to use Qt..)
From debugger: "Program received signal SIGSEGV, Segmentation fault."
And a picture in comment. (Need more reputation for 3 links..)
sorry, I'm very new and have to hurry, so I just wanted to put this up. The code randomly ends after trying the first input (choosing what topic, algebra, basic mathematics, etc.) I've tried other sources, tried reformatting and still don't know what it is. I'm fairly new, so I assume it's just a stupid issue that I overlooked.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
int calculatorChoice, choiceMath1;
int addition1, addition2, additionSum;
int subtraction1, subtraction2, subtractionDifference;
int multiplication1, multiplication2, multiplicationProduct;
int division1, division2, divisionQuotient;
cout << "What would you like to figure out?" << endl;
cout << "" << endl;
cout << "[1]: Basic Mathematic Equations" << endl;
cout << "[2]: Figuring out a Grade" << endl;
cout << "[3]: Algebra" << endl;
cout << "[4]: Inquiry Sciences" << endl;
cout << "[5]: Unit Conversion" << endl;
cin >> calculatorChoice;
if (calculatorChoice == 1) {
cout << "What would you like to do?" << endl;
cout << "" << endl;
cout << "[1]: Addition" << endl;
cout << "[2]: Subtraction" << endl;
cout << "[3]: Multiplication" << endl;
cout << "[4]: Division" << endl;
cin >> choiceMath1;
if (choiceMath1 == 1) {
cout << "Choose your first number." << endl;
cin >> addition1;
cout << "Choose your second number." << endl;
cin >> addition2;
additionSum = addition1 + addition2;
cout << "The sum of " << addition1 << " and " << addition2 << " is " << additionSum << "." << endl;
}
else if (choiceMath1 == 2) {
cout << "Choose the first number." << endl;
cin >> subtraction1;
cout << "Choose the second number." << endl;
cin >> subtraction2;
subtractionDifference = subtraction1 - subtraction2;
cout << "The difference of " << subtraction1 << " and " << subtraction2 << " is " << subtractionDifference << "." << endl;
}
else if(choiceMath1 == 3) {
cout << "Choose the first number." << endl;
cin >> multiplication1;
cout << "Choose the second number." << endl;
cin >> multiplication2;
multiplicationProduct = multiplication1 * multiplication2;
cout << "The product of " << multiplication1 << " and " << multiplication2 << " is " << multiplicationProduct << "." << endl;
}
else if(choiceMath1 == 4) {
cout << "Choose the first number." << endl;
cin >> division1;
cout << "Choose the second number." << endl;
cin >> division2;
divisionQuotient = division1 / division2;
cout << "The quotient of " << division1 << " by " << division2 << " is " << divisionQuotient << "." << endl;
}
else {
cout << "That is not a choice." << endl;
}
}
else {
}
}
void calculator() {
}
Your code doesnt have any loop, what means, after enter the input , it shows the result and program finishes. If you want to stop your code somewhere (depends on the IDE, this solution is for VS) just type system("pause")and you will see what your program shows on console. In case you are not on VS, add #include <cstdlib>
If you explain a bit more hat you expect your code to do we can help you easier.
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
Hi so i have started c++ 3 days ago, read some tutorials etc. I wanted to make my own hangman game since it seemed as an easy task for a begginer but i stumbled upon an issue. Everything is working well ecept i cant seem to find a way to make 1 letter strings swap with underscores until underscores change to the missing word. So basically when you compile it you can guess the whole word only.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess;
int wrong=0;
int main (){
string copy = word;
cout << "----------------------Hello! Welcome to the HANGMAN game!---------- ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;
cin >> word;
//space
for (int x=0; x<30; x++){
cout << endl;
}
//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('_');}
cout << underscore << endl;
//MAIN WHILE
while(wrong<12){
cin >> guess;
//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << usedguess << endl;
wrong ++;
}
}
if(underscore == word){
cout << "You win!" << endl;
break;
}
if(wrong==1){
cout << "I" << endl;
}
else if(wrong==2){
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==3){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==4){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==5){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==6){
cout << "I===" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==7){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==8){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I |" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==9){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==10){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==11){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I /" << endl;
cout << "I" << endl;
}
else if(wrong==12){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I / /"<< endl;
cout << "I YOU ARE DEAD" << endl;
cout << "Game over bro! The word was: " << word <<endl;
break;
}
}
}
To compare the strings of word and guess, you can iterate over the characters in a for-loop, and check if there is a match
string word = "hangman";
string guess = "mansomething";
string underscore = string(word.size(), '_'); // init a string with underscores equal to the length of 'word'
// iterate over the characters in word and guess
for (size_t i = 0, iend = min(word.size(), guess.size()); i < iend; i++) {
if (word[i] == guess[i])
underscore[i] = word[i]; // if the characters match at position i, update the underscore.
}
cout << underscore << endl;
Afterwards, underscore contains the following
_an____
Basically I want an exact copy of the code that appears in the console window to also be outputted to a txt file..
#include <conio.h>
#include <iostream>
#include <dos.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <fstream>
using namespace std;
//Initialising gotoxy Comand
void gotoxy(int col, int row)
{
COORD coord;
coord.X = col;
coord.Y = row;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
char name1[20], name2[20], name3[30], name4[20];
int funcNum = 0;
int n1Nv, n1Mv, n1SEv, n1SWv;
int n2Nv, n2Mv, n2SEv, n2SWv;
int n3Nv, n3Mv, n3SEv, n3SWv;
int n4Nv, n4Mv, n4SEv, n4SWv;
int n1Total, n2Total, n3Total, n4Total, perTotal;
double n1Per, n1PerTotal;
double n2Per, n2PerTotal;
double n3Per, n3PerTotal;
double n4Per, n4PerTotal;
double maxVote;
//Introduction
cout << "================================================================================";
cout << " Ballot Results" << endl;
cout << " Version 2.1" << endl;
cout << " Created by Team b0nkaz" << endl;
cout << "================================================================================" << endl;
//Candidate Identification
cout << "Enter the candidates running for president" << endl << endl;
//cin.getline (workaround,30); //**
cout << "Candidate One: ";
cin.getline (name1,20);
cout << "Candidate Two: ";
cin.getline (name2,20);
cout << "Candidate Three: ";
cin.getline (name3,20);
cout << "Candidate Four: ";
cin.getline (name4,20);
cout << " " << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
cout << "Input vote numbers from each region pressing enter after each input:" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
//Input Table
//Regions
gotoxy(22,19);
cout << "North" << endl;
gotoxy(31,19);
cout << "Midlands" << endl;
gotoxy(43,19);
cout << "South East" << endl;
gotoxy(57,19);
cout << "South West" << endl;
gotoxy(69,19);
cout << "| Total" << endl;
cout << "_____________________________________________________________________|__________" << endl;
gotoxy(69,21);
cout << "|";
gotoxy(69,22);
cout << "|";
gotoxy(69,23);
cout << "|";
gotoxy(69,24);
cout << "|";
gotoxy(69,25);
cout << "|";
gotoxy(69,25);
cout << "|";
gotoxy(69,26);
cout << "|";
gotoxy(69,27);
cout << "|";
gotoxy(69,28);
cout << "|";
gotoxy(69,29);
cout << "|";
//Candidates
gotoxy(0,22);
cout << name1;
gotoxy(0,24);
cout << name2;
gotoxy(0,26);
cout << name3;
gotoxy(0,28);
cout << name4;
//Equals
cout << endl;
cout << "_____________________________________________________________________|__________" << endl;
//Vote Input
//North
gotoxy(22,22);
cin >> n1Nv;
gotoxy(22,24);
cin >> n2Nv;
gotoxy(22,26);
cin >> n3Nv;
gotoxy(22,28);
cin >> n4Nv;
//Midlands
gotoxy(31,22);
cin >> n1Mv;
gotoxy(31,24);
cin >> n2Mv;
gotoxy(31,26);
cin >> n3Mv;
gotoxy(31,28);
cin >> n4Mv;
//South East
gotoxy(43,22);
cin >> n1SEv;
gotoxy(43,24);
cin >> n2SEv;
gotoxy(43,26);
cin >> n3SEv;
gotoxy(43,28);
cin >> n4SEv;
//South West
gotoxy(57,22);
cin >> n1SWv;
gotoxy(57,24);
cin >> n2SWv;
gotoxy(57,26);
cin >> n3SWv;
gotoxy(57,28);
cin >> n4SWv;
//Total Votes
//Name1
gotoxy(72,22);
n1Total = n1Nv + n1Mv + n1SEv + n1SWv;
cout << n1Total;
//Name2
gotoxy(72,24);
n2Total = n2Nv + n2Mv + n2SEv + n2SWv;
cout << n2Total;
//Name3
gotoxy(72,26);
n3Total = n3Nv + n3Mv + n3SEv + n3SWv;
cout << n3Total;
//Name4
gotoxy(72,28);
n4Total = n4Nv + n4Mv + n4SEv + n4SWv;
cout << n4Total << endl << endl << endl;
//Percentage Calculation
perTotal = n1Total + n2Total + n3Total + n4Total;
//Candidate One
n1Per = n1Total*100;
n1PerTotal = n1Per/perTotal;
//Candidate Two
n2Per = n2Total*100;
n2PerTotal = n2Per/perTotal;
//Candidate Three
n3Per = n3Total*100;
n3PerTotal = n3Per/perTotal;
//Candidate Four
n4Per = n4Total*100;
n4PerTotal = n4Per/perTotal;
cout << "Please wait for calculation..." << endl << endl;
//Spinning Loading Line
//std::cout << '-' << std::flush;
//for(;;)
//{
//Sleep(100);
//std::cout << "\b\\" << std::flush;
//Sleep(100);
//std::cout << "\b|" << std::flush;
//Sleep(100);
//std::cout << "\b/" << std::flush;
//Sleep(100);
//std::cout << "\b-" << std::flush;
//}
//Sleeping Program
Sleep(1500); //1.5 secs
//Total Output
cout << "Candidate percentage:" << endl << endl;
//Converting To One Decimal Place
cout << fixed;
std::cout.precision(1);
//Vote Percentages
cout << name1 << " = " << n1PerTotal << "%" << endl;
cout << name2 << " = " << n2PerTotal << "%" << endl;
cout << name3 << " = " << n3PerTotal << "%" << endl;
cout << name4 << " = " << n4PerTotal << "%" << endl << endl;;
//Calculating Winnner
maxVote=n1PerTotal;
if (n2PerTotal>maxVote)
maxVote=n2PerTotal;
if (n3PerTotal>maxVote)
maxVote=n3PerTotal;
if (n4PerTotal>maxVote)
maxVote=n4PerTotal;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
//Sleeping Program
Sleep(1500); //1.5 secs
if(maxVote==n1PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name1 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n1PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n2PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name2 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n2PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n3PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name3 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n3PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n4PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name4 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n4PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
cout << "Press any key to exit..." << endl;
getch();
//system("pause");
}
I am using MS Visual Studio 2010 and any help would be great! Please note I am still very new to C++.
EDIT I would like to be able to see the output in CMD as well as have a separate txt file with the same output code. Everything that is displayed in the CMD window also copied into the txt file.
use an ofstream operator as such.
For some reason I can't seem to find the comment button or I would be involved in the discussion above... but I've edited my code below to reflect what seem to be some concerns of yours.
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("data.txt"); //Will create a new file if one is not already in existence
//You can put a static filename here or have them enter a string
//If you use a custom string your input will be "fout.open(STRING.c_str());
fout<<"Used exactly like cout from this point on";
fout.close(); //When you are done using it to close the file
}
I chose to name my ofstream operator fout because (while this is not always good to do) this way you can quickly change all of your cout's to fout's, or replicate them.
If the user enters a value and you want to spit it back out as well, you can use the ofstream operator after every cin.
You can find more information about ofstream here...
Hope this helped!
You can do this from outside of the application using tee. tee takes the output of a program and splits it into two streams (e.g. one to stdout and one to a file). Unfortunately, Windows doesn't come with a tee command but there are many implementations available.
If you want this to happen from within your program you can do the equivalent of tee using a custom ofstream. This article explains how.