I am trying to make a random number generator guessing game, which will allow the user to pick a range. After the range is picked the user will try to guess the number until they get it right. I am new to c++ and do not understand why two cout outputs on screen. I am new to c++ and cant figure out why. I am using visual studio 17.
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
int userNum;
char choice;
int numattempts = 0;
int randNumber;
srand(time(0));
cout << "Welcome to the number guessing game\n" << "Please choose what range you want to guess from\n" << " '1' = 0-20\n '2'= 0-50\n '3'= 0-100\n ";
cin >> choice;
if (choice == '1')
{
randNumber = 0 + rand() % 20;
cout << "Enter a number between 0 and 20\n";
cin >> userNum;
cout << randNumber;
do {
if (userNum < randNumber)
{
cout << "Number is Higher\n";
cin >> userNum;
}
else if (userNum > randNumber)
{
cout << "Number is Lower\n";
cin >> userNum;
}
else (userNum == randNumber);
{
cout << "Congrats You got it!\n";
}
} while (userNum != randNumber);
}
you miss another if in the last checking and should not put semicolon there.
try to debug and you'll find that it should be like this
do {
if (userNum < randNumber)
{
cout << "Number is Higher\n";
cin >> userNum;
}
else if (userNum > randNumber)
{
cout << "Number is Lower\n";
cin >> userNum;
}
else if(userNum == randNumber)
{
cout << "Congrats You got it!\n";
}
} while (userNum != randNumber);
Related
I have to write a program in C++ that asks user to enter lines and store them in a text file.
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
}
The problem is that the text is not being stored in different lines.
I have to use Turbo C++ for this program.
Minimal, Reproducible Example:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
void show(char name[80])
{
char line[800];
cout << "Contents of the file:" << endl << endl;
ifstream fin (name);
while(!fin.eof())
{
fin.getline(line,80);
if(fin.eof())
break;
cout << line;
}
}
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
fflush(stdin);
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
fflush(stdin);
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
show(name);
}
int main()
{
char name1[80];
cout <<"Enter the name of the text file:" << endl;
gets(name1);
create_file(name1);
return 0;
}
I've try this case but I got stuck in a trouble. I just want to to determine length of a string from user input or from console. But it doesn't work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int N;
string S;
char vocal;
vocal = {'a'||'i'||'u'||'e'||'o'};
cout << "Length of Spaghetti Name: ";
cin >> N;
fflush(stdin);
cout << "Spaghetti Name: ";
cin >> S;
if (S[0] != vocal){
S.erase(0,1);
} else if (S[N] != vocal){
S.erase(N,1);
}
cout << S << endl;
cin.get();
return 0;
}
expected input
9
Carbonara
expected output
arbonara
This program will delete non vocal character in spaghetti names.
Could anyone help me to upgrade my code ?
I bet the function could be done in shorter and more elegant way, could you show me the proper direction or examples how to write that?
Thanks for the replies, here's the code ( written in Codeblocks 16.01):
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int D,a,b,bb,liczba,dlugosc;
int main()
{
cout << "liczba testow: ";
cin >> D;
while(D<11 && D>0){
for (int j=0;j<D;j++){
cin >> a >> b;
if(a<0 | a>1000000000 | b<0 | b>1000000000){
break;
}
liczba =a;
for(int i=1;i<b;i++){
liczba = liczba*a;
}
char aa[10] = {0};
itoa(liczba,aa,10);
string bb = aa;
int dlugosc = bb.length();
cout << bb << endl;
cout << bb[dlugosc-1] << endl;
a = 0;
}
return 0;
}
return 0;
}
O.F community! I'm very new to c++ and I decided to create my own game.
My issue is that at one point in the game the player is asked to answer 3 things:
-Gender
-Age
-Setting
void assessment()
{
cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
cout << "Press any key to continue\n\n";
cin >> anykey;
cout << "You are a " << ageNumber << " year old " << gender; //incomplete - needs //<< setting;
}
The issue is with gender not being displayed. It can cout << ageNumber fine. But when the game is played and this part is reached this is what it says:
Now that the characteristics have been selected. Let's review what they are:
Press any key to continue
1
You are a 16(that is what I put for ageNumber earlier on) year old . <- Notice how "old" and "." have a space? As if the string gender is read as a blank.
EDIT: It is supposed to say:
You are a 16 year old male/female/it.
This is the part that determines the gender.
void fate()
{
cout << "Before we begin, a few characteristics must be selected\n\n";
cout << "Gender?\n\n";
cout << "1. Male\n";
cout << "2. Female\n";
cout << "3. Other\n\n";
cin >> genderNumber;
cout << "Now your next characteristic...Press any key to continue\n\n";
cin >> anykey;
cout << "What is your age? (Must be younger than 50 to play)\n\n";
cin >> ageNumber;
setting2();
if (genderNumber == 1)
{
string gender = "male";
}
else if (genderNumber == 2)
{
string gender = "female";
}
else if (genderNumber == 3)
{
string gender = "'it'";
}
else
{
"invalid input\n\n\n\n\n\n";
fate();
}
===========================================================================================
This is the full code:
#include <iostream>
#include <string>
using namespace std;
void rules();
void controls();
void start();
void fate();
void assessment();
void setting2();
int fateNumber;
int anykey;
int ageNumber;
int settingNumber;
int genderNumber;
string gender;
string setting;
int main(int argc, char** argv)
{
cout << "===W E L C O M E T O C H O O S E Y O U R F A T E=== \n\n";
cout << "Press any key to continue\n";
cin >> anykey;
start();
}
void controls()
{
cout << "Throughout this game will you be making choices, each choice will have a corresponding number next to it. Press the letter to the corresponding choice you want to make. Press any key to continue\n";
cin >> anykey;
cout << "Here is an example...you are a cat, you can either\n\n";
cout << "1. Meow\n\n";
cout << "2. Hiss\n";
cout << "Press any key to continue\n";
cin >> anykey;
cout << "If I wanted to Hiss I would press the 2 key\n\n";
cout << "Press any key to continue on to the rules\n\n";
cin >> anykey;
rules();
}
void rules()
{
cout << "#1 You will be given a set number of choices. Press any key to continue\n\n";
cin >> anykey;
cout << "#2: There are 2 factors to this game. Press any key to continue\n\n";
cin >> anykey;
cout << "#3: The first factor is that either you live or you die. Press any key to continue\n\n";
cin >> anykey;
cout << "#4: The second factor is that if you live, your character will be judged based on events you've made. You can still live, but you might have a good or bad effect on the people around you. Press any key to continue\n\n";
cin >> anykey;
cout << "Let's play shall we?";
}
void start()
{
cout << "You shall begin a journey that'll decide either you make it or you don't. Press any key to continue\n\n";
cin >> fateNumber;
cout << "But before we start...The game needs to be explained.\n\n\n\n";
cout << "Controls(1 key)---------------------------Rules(2 key)---------------Skip(3 key) \n\n";
cin >> fateNumber;
if (fateNumber == 1)
{
controls();
}
else if (fateNumber == 2)
{
rules();
}
else if (fateNumber == 3)
{
fate();
}
else
{
cout << "invalid key\n\n\n\n\n\n\n";
start();
}
}
void fate()
{
cout << "Before we begin, a few characteristics must be selected\n\n";
cout << "Gender?\n\n";
cout << "1. Male\n";
cout << "2. Female\n";
cout << "3. Other\n\n";
cin >> genderNumber;
cout << "Now your next characteristic...Press any key to continue\n\n";
cin >> anykey;
cout << "What is your age? (Must be younger than 50 to play)\n\n";
cin >> ageNumber;
setting2();
if (genderNumber == 1)
{
string gender = "male";
}
else if (genderNumber == 2)
{
string gender = "female";
}
else if (genderNumber == 3)
{
string gender = "'it'";
}
else
{
"invalid input\n\n\n\n\n\n";
fate();
}
if (ageNumber >= 50 && ageNumber < 100)
{
cout << "Didn't you read the age rating dumbass? Younger than 50...\n\n";
}
else if (ageNumber < 50 && ageNumber > 22)
{
setting2();
}
else if (ageNumber <= 22 && ageNumber >= 16)
{
setting2();
}
else if (ageNumber < 16 && ageNumber >= 0)
{
setting2();
}
else if (ageNumber >= 100)
{
cout << "More than a century old and you expect me to believe you got this far? Bullshit.";
}
else
{
cout << "Error - Invalid number\n\n\n\n\n\n";
cout << "what is your age again?\n\n";
cin >> ageNumber;
}
}
void assessment()
{
cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
cout << "Press any key to continue\n\n";
cin >> anykey;
cout << "You are a " << ageNumber << " year old " << gender << ". You are " << setting << endl << endl;
if (settingNumber == 1)
{
string setting = "home";
}
else if (settingNumber == 2)
{
string setting = "jail";
}
}
void setting2()
{
cout << "Alright and lastly pick your setting\n\n";
cout << "1. Home sweet home\n";
cout << "2. Vacation \n";
cout << "3. Jail\n\n";
cin >> settingNumber;
assessment();
}
=======================================================================================
Any help is appreciated ^^ Code on!
Output (thru ofstream-s like cout) is buffered in C++. You want
cout << flush;
or
cout << endl;
to ask the buffer to be flushed (endl manipulator also emits a new line but flush does not).
So code:
cout << "You are a " << ageNumber << " year old " << gender << flush;
or
cout << "You are a " << ageNumber << " year old " << gender << endl;
Also, your gender should be declared at a wider scope:
if (genderNumber == 1) {
string gender = "male";
}
should become (to refer to the global gender)
if (genderNumber == 1) {
gender = "male";
}
BTW, if you enabled all warnings with your compiler (e.g. compile with g++ -g -Wall -Wextra, if using GCC, e.g. on Linux) you should have been warned. And of course, you should use the debugger (e.g. gdb, at least on Linux) and step into your code with it.
i am slightly rusty on programming and i was asked to write a c++ program that would read and print from a text file whole lines that include one, both or none of the words given by the user.
this is what i have until now
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string string1,string2;
ifstream in_stream;
char choice;
void main()
{
//oppening the file needed
in_stream.open("file.txt");
//giving the strings to search
cout << "Give two strings. " << endl;
cin >> string1;
cin >> string2;
// choosing which mode to follow
cout << "Choose which mode you prefer: " << endl;
cout << "A: Both words in each line." << endl;
cout << "B: One of the words in each line." << endl;
cout << "C: None of the words in each line." << endl;
cin >> choice;
switch (choice)
{
case 'A': cout << choice << endl;
break;
case 'B': cout << choice << endl;
break;
case 'C': cout << choice << endl;
break;
default: cout << "Incorrect choice" << endl;
break;
}
system ("pause");
}
Try this implementation:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
string string1,string2;
ifstream in_stream;
char choice;
void printLine(const vector<string>& _line)
{
for (const auto& word : _line)
cout << word << " ";
cout << endl;
}
void main()
{
//oppening the file needed
in_stream.open("file.txt");
vector<vector<string> > lines;
lines.push_back(vector<string>());
while (in_stream.good())
{
string1.clear();
in_stream >> string1;
if (!string1.empty())
lines.back().push_back(string1);
if (in_stream.good())
{
if (in_stream.peek() == '\n')
lines.push_back(vector<string>());
}
}
//giving the strings to search
cout << "Give two strings. " << endl;
cin >> string1;
cin >> string2;
// choosing which mode to follow
cout << "Choose which mode you prefer: " << endl;
cout << "A: Both words in each line." << endl;
cout << "B: One of the words in each line." << endl;
cout << "C: None of the words in each line." << endl;
cin >> choice;
switch (choice)
{
case 'A': cout << choice << endl;
for (const auto& line : lines)
{
if (find(line.begin(), line.end(), string1) != line.end() && find(line.begin(), line.end(), string2) != line.end())
printLine(line);
}
break;
case 'B': cout << choice << endl;
for (const auto& line : lines)
{
if (find(line.begin(), line.end(), string1) != line.end() || find(line.begin(), line.end(), string2) != line.end())
printLine(line);
}
break;
case 'C': cout << choice << endl;
for (const auto& line : lines)
{
if (find(line.begin(), line.end(), string1) == line.end() && find(line.begin(), line.end(), string2) == line.end())
printLine(line);
}
break;
default: cout << "Incorrect choice" << endl;
break;
}
system("pause");
}