I need to have spaces between outputted _ - c++

I have just more or less finished my first C++ Project, it is a Hangman Game and so far everything works fine. The only Problem is that i need to have spaces between the underlines (_) that represent the hidden word. If anyone could help me on this i would really appreciate it.
// UNCOMMENT THE FOLLOWING LINE (REMOVE THE TWO SLASHES AT THE BEGINNING) TO RUN AUTOMATIC TESTS
#include "tests.h"
#include <iostream>
#include <string>
#include "hangman.h"
int main(){
using namespace std;
// display the hidden word
std::string word_to_guess = chooseWord();
int misses = 0;
std::string displayed_word = word_to_guess;
for(int i=0; i< displayed_word.length(); i++)
displayed_word[i] = '_';
int attempts = 6;
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for correct letter
while(1){
std::cout << "Your guess";
std::cout << ":";
char guess;
std::cin >> guess;
bool Correct = false;
for(int i=0; i< word_to_guess.length(); i++)
if (guess == word_to_guess[i]) {
displayed_word[i] = word_to_guess[i];
Correct = true;
}
if (!Correct)
attempts--;
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for win or lose
if (attempts==0)
std::cout << "The word was: " << word_to_guess << std::endl << "You lost!";
if (attempts==0)
return 0;
if (!word_to_guess.find(displayed_word))
std::cout << "You won!";
if (!word_to_guess.find(displayed_word))
return 0;
}
}

First, you can simplify this
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
Now, about your question, I think a best solution is replacing
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "[";
for(int i = 0; i < displayed_word.length(); i++) {
if(i == 0 || displayed_word[i] == '_')
std::cout << " ";
std::cout << displayed_word[i];
if(i == displayed_word.length()-1 || (displayed_word[i] == '_' && displayed_word[i+1] != '_'))
std::cout << " ";
}
std::cout << "]" << std::endl;
Explaination:
We put spaces at the beginning and the end, and also around underscores, but we make sure to put only one space between two underscores.

Related

undefined reference to `Mosaic::Mosaic()'

Error in terminal of VS Code:
/tmp/ccU4qzPn.o: In function main':
azul.cpp:(.text+0xa7e): undefined reference toMosaic::Mosaic()'
azul.cpp:(.text+0xa86): undefined reference to `Mosaic::printMosaic()'
collect2: error: ld returned 1 exit status*
azul.cpp
#include "Mosaic.h"
#include <iostream>
// int menuOption;
std::string playerOne;
std::string playerTwo;
void printMenu() {
std::cout << " " << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "| Welcome to Azul |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << "| ----------- |" << std::endl;
std::cout << "| Main Menu |" << std::endl;
std::cout << "| ----------- |" << std::endl;
std::cout << "| 1. New Game |" << std::endl;
std::cout << "| 2. Load Game |" << std::endl;
std::cout << "| 3. Credits |" << std::endl;
std::cout << "| 4. Help |" << std::endl;
std::cout << "| 5. Quit |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << " " << std::endl;
}
int main(void) {
printMenu();
std::cout << "Enter Your Choice: " << std::endl;
char menuOption;
std::cin >> menuOption;
if (menuOption == '1') {
std::cout << "\n\nStarting a new game..." << std::endl;
std::cout << "\nEnter Name for Player One:" << std::endl;
std::cin >> playerOne;
std::cout << "\nEnter Name for Player Two:" << std::endl;
std::cin >> playerTwo;
std::cout << "\n\nLet's play!\n" << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "Start Round" << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "\nMosaic for " << playerOne << ":" << std::endl;
Mosaic* mosaic = new Mosaic();
mosaic->printMosaic();
}
return EXIT_SUCCESS;
};
Mosaic.cpp
#include "Mosaic.h"
#include <iostream>
Mosaic::Mosaic(){
}
Mosaic::~Mosaic(){
}
void Mosaic::printMosaic() {
for (int i = 1; i < 6; i++)
{
std::cout << i << ". ";
for (int j = 6; j > i; j--)
{
std::cout << " ";
}
for (int j = 0; j < i; j++)
{
std::cout << ".";
}
std::cout << "|| . . . . ." << std::endl;
}
}
Mosaic.h
class Mosaic
{
public:
Mosaic();
~Mosaic();
void printMosaic();
};
I think the problem is at the pointer
Mosaic* mosaic = new Mosaic();

How can I remove values from output statement given a condition? - c++

I'm a little confused as to how to go around this. I have some array variables with some information, and I want to print them out after some calculations. If the value is 0, then I want to print a " " instead. There are 3 arrays that need to get checked however, how would I change the output statement to cater for all 3 checks and print an empty string instead of the value?
for(int start = 1; start < 13; start++)
{
if(check[start] == 1)
{
cout << checkMonth(start) << ": " << setprecision(1) << fixed << averagespeed[start] << "(" << setprecision(1) << fixed << sdSpeed[start] << ")," << setprecision(1) << fixed << averagetemp[start] << "(" << setprecision(1) << fixed << sdTemp[start] << ")," << setprecision(1) << fixed << Solar[start] << '\n';
}
/*if(sumTemp[start] == 0 || sumTemp[start] == 0 || sumSpeed[start] == 0){
}*/
}
Example Output looks like this:
January,5.5(1.2),25.5(12.2),196.4
For example if Sum of Speed is 0, that means all values of speed were 0 or null. So it should change to this:
January,,25.5(12.2),196.4
A single line to std::cout doesn't need to be done in one statement. For example:
std::cout << "First";
std::cout << ", second"
std::cout << ", third\n"
Prints the following line:
First, second, third
Now we can use an if to conditionally print the middle part of the string:
std::cout << a;
if (b != 0) {
std::cout << ", " << b;
}
std::cout << ", " << c << '\n';

simple game, while loop and checking strings

I am a beginner at c++ and I want to create simple game. You have vector of strings, then you check if line input matched the right answer.
I want to generate random number 1 ,2 or 3. Then check if line matches correct answer and count the points.
I am probably missing something basic, yet I dont know what.
Problems:
Input line get correctly read on only first iterations
somehow points (tocke) jumps to 45763 after finishing.
At beginning time (cas) is sometimes 2.
Code:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <string>
int main() {
int runde;
int tocke;
int cas;
std::cout << "\n" << "Pravila igre:" << "\n" << "Za pravilen odgovor dobis 1 tocko, za napacnega zgubis 2!"<<
"\n" << "Stevilo zivljenj si izberes sama!"<< "\n" << "\n" ;
std::cout << "Izberi stevilo zivljenj!:" << "\n";
std::cin >> runde ;
std::vector<std::string> latin = {"carum carvi", "artemisia absiinthium","coriandrum sativum"};
std::vector<std::string> slovene = {"navadna kumina", "pravi pelin", "koriander"};
tocke << 0;
cas << 0;
do {
int ind;
cas << cas + 1;
std::cout << "Round " << cas <<"! Ladies and gentlemans, buckle your seatbelts!"<<"\n" << "\n" ;
ind = std::rand() % 3;
std::cout << "ime rastline: " << slovene[ind] << "\n";
std::cin.ignore();
std::string line;
getline(std::cin, line);
std::cout << "\n";
if (latin[ind] == line){
std::cout << "Pravlino! Tocka zate!" << "\n";
tocke << tocke + 1;
std::cout << "Tocke == " << tocke << "\n" << "Zivjenja == " << runde << "\n" << "Prezivete runde == " << cas << "\n"<< "\n";
}
else
{
std::cout << "Napaka! :D" << "\n";
std::cout << "Pravilen odgovor == " << latin[ind] << "\n";
-- runde ;
tocke << tocke - 2;
std::cout << "Tocke == " << tocke << "\n" << "Zivjenja == " << runde << "\n" << "Prezivete runde == " << cas << "\n"<< "\n";
}
}while(runde >= 0 );
std::cout << "\n"<<"Stevilo tock == " << tocke <<"\n" << "St. prezivetih rund == " << cas - 1
<< "\n" ;
}
You seem to have a misconception regarding operators. << is NOT assignment, use = instead. So tocke << 0; doesn't assign 0 to tocke, it does bitshifting (on an uninitialized variable), then discards the result. tocke stays uninitialized and this causes problems later.
Instead of this:
tocke << 0;
cas << 0;
Do this:
tocke = 0;
cas = 0;
Also instead of cas << cas + 1; do cas++ and instead of tocke << tocke - 2; do tocke -= 2;. To learn how the assignment operators work, you can read about them here. Last but not least, try to see if your compiler gives you any warnings, it should complain about using uninitialized values.

GetConsoleSelectionInfo C++

cI want to select some text with my cursor using the Mark Function from Console, but my code doesn't work ...
CONSOLE_SELECTION_INFO c;
if(GetConsoleSelectionInfo(&c))
{
while((c.dwFlags & CONSOLE_MOUSE_DOWN) == 0) { if(c.dwFlags) cout << c.dwFlags; }
cout << "SelectionAnchor: " << c.dwSelectionAnchor.X << " " << c.dwSelectionAnchor.Y;
cout << "RectangleSelection: " << c.srSelection.Top << " " << c.srSelection.Left << c.srSelection.Bottom << c.srSelection.Right;
}
else cout << "\n\nError: " << GetLastError();
Whatever I'm selecting or I'm doing, always c.dwFlags will be 0 ...

While loop condition not working

Well basically the condition for healthtwo causes the program to stop but not healthone for some reason
complete code: http://en.textsave.org/CmN
if (chance<=rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo=healthtwo-attackp;
}
else {
cout << " " << endl;
cout << "Miss!" << endl;
}
chance=1+rand()%23;
if (chance<=rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone=healthone-attackcp;
}
else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
} while (healthone>=0 || healthtwo>=0);
That should be a logical and (&&). After all, you want to check whether the health of both contestants is greater or equal to zero.