C++ outputting weird [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
cout << "*****************************************" <<
endl <<
cout << "Hello All!" <<
endl <<
cout << "Welcome to CSCI-111!!!!!" <<
endl <<
cout << "It is great to see you!" <<
endl <<
cout << "*****************************************" ;
return 0;
}
The first cout is fine and outputs correctly, but every cout after that outputs a strange string of numbers before the words in quotation marks (0x600e88) and my output ends up looking like this
*****************************************
0x600e88Hello All!
0x600e88Welcome to CSCI-111!!!!!
0x600e88It is great to see you!
0x600e88*****************************************

What you currently have:
cout << "blah" << endl << cout << "blah" << endl << cout << ... ;
// ^~~~ ^~~~
You're printing the cout itself, this is what gives you weird numbers.
What you should have:
cout << "blah" << endl;
cout << "blah" << endl;
Or:
cout << "blah" << endl
<< "blah" << endl;

Terminate endls:
cout << "*****************************************" << endl;
cout << "Hello All!" << endl;
cout << "Welcome to CSCI-111!!!!!" << endl;
cout << "It is great to see you!" << endl;
cout << "*****************************************" ;
Or delete the redundant couts:
cout << "*****************************************" << endl <<
"Hello All!" << endl <<
"Welcome to CSCI-111!!!!!" << endl <<
"It is great to see you!" << endl <<
"*****************************************" ;
Otherwise, the expression continues, and you print cout itself, and since it's a function pointer, you print it's address (0x600e88).
In the sequence
cout << "Something" << endl << cout;
the first cout indicates the start of ostream (a stream printing out to the console), while the second one is the part of the stream you want to output, and is treated as a pointer, who outputs what he contains - a numeric address of the call to cout.

Related

Reading a full line of text from .txt with white space into a single variable C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having some trouble reading from a file.
My file contains multiple lines that all need to be saved into variables of different types. I was able to get the bools and the ints to transfer properly but I am having trouble with my strings, specifically strings that contain white space.
My file contains:
1
0
1
0
0
0
1
0
0
0
Chester Von Tester
1
1
I was able to get each line to properly read into individual variables but it freaks out when it gets to Chester Von Tester, either only inputting the first word or nothing at all depending on which method I use.
Here is the tidbit of code thats not working right (the entirety of code is a few hundred lines, don't think it is necessary to post):
loadGame >> newGame;
cout << typeid(newGame).name() << " " << newGame << endl;
loadGame >> bossOne;
cout << typeid(bossOne).name() << " " << bossOne << endl;
loadGame >> bossTwo;
cout << typeid(bossTwo).name() << " " << bossTwo << endl;
loadGame >> bossThree;
cout << typeid(bossThree).name() << " " << bossThree << endl;
loadGame >> bossFour;
cout << typeid(bossFour).name() << " " << bossFour << endl;
loadGame >> bossFive;
cout << typeid(bossFive).name() << " " << bossFive << endl;
loadGame >> bossDeathCount;
cout << typeid(bossDeathCount).name() << " " << bossDeathCount << endl;
//Position Coordinates
loadGame >> coord_x;
cout << typeid(coord_x).name() << " " << coord_x << endl;
loadGame >> coord_y;
cout << typeid(coord_y).name() << " " << coord_y << endl;
loadGame >> inCombat;
cout << typeid(inCombat).name() << " " << inCombat << endl;
//Character traits
getline(loadGame,characterName);
cout << typeid(characterName).name() << " " << characterName << endl;
loadGame >> characterGender;
cout << typeid(characterGender).name() << " " << characterGender << endl;
loadGame >> characterClass;
cout << typeid(characterClass).name() << " " << characterClass << endl;
Here is what happens when the code runs:
b 1
b 0
b 1
b 0
b 0
b 0
i 1
i 0
i 0
b 0
Ss
i 0
i 1
It is getting all the type id's correct, which is a start. Where it's messing up is the name where it says "Ss" then a blank when it should say "Ss Chester Von Tester" followed by "i 1" not "i 0".
What would be the best way to get the entire line "Chester Von Tester" into that string variable?
With out posting the code, it is very difficult to understand what you are trying to do. You can use std::getline function which will read one line per a function call.

C++ special characters displays as "?"

I want to print a box using special characters like this
cout << "╔═══╗" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "╚═══╝" << endl;
but it displays like this
?????
? ?
? ?
?????
How can I fix this?
You could try this one:
cout << (char)201 << (char)205 << (char)187 << endl;
cout << (char)186 << " " << (char)186 << endl;
cout << (char)186 << " " << (char)186 << endl;
cout << (char)200 << (char)205 << (char)188 << endl;
I tested it and prints what you want
Find out what character set the terminal you are viewing the program output on is using, then use escape codes to put those characters in your strings
There are several types and editor you are programming with may be using a different kind the program displays with.
https://en.wikipedia.org/wiki/Box-drawing_character

Can't use void() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i'm having an issue with my C++ code:
#include <iostream>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
void displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints) // Call this function to display
{ // ennemy related informations
cout << endl << "Ennemy health points: " << ennemyHealthPoints;
cout << endl << "Ennemy attack points: " << ennemyAttackPoints << endl;
}
int main()
{
//Player related variables
int healthPointsMax(100);
int actionPointsMax(100);
int recoveryPoints(100);
int healthPoints(100);
int actionPoints(100);
int attackPoints(100)
//Player related variables
//Ennemy related variables
int ennemyHealthPoints(230);
int ennemyAttackPoints(10);
//Ennemy related variables
//Main variables
string stringInput;
//Main variables
//TEXT
cout << "HP: " << healthPoints << endl;
cout << "AP: " << actionPoints << endl;
cout << "RP: " << recoveryPoints << endl;
cout << endl;
cout << "HP = Health Points, AP = Action Points, RP = Recovery Points" << endl;
cout << endl;
cout << "CONTROLS:" << endl;
cout << "attack [ennemy name] //attacks the ennemy" << endl;
cout << "heal [playername] // heals the selected player" << endl;
cout << endl;
cout << "A wild nugget appears!" << endl;
cout << endl;
cout << "What do you want to do?" << endl;
// TEXT
getline(cin, stringInput);
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);
}
return 0;
}
The debugger says that there is a problem with the fonction "void displayEnnemyStatus"
I checked every variable, there is no problem with that.
Am I doing something wrong?
Try :-
if (stringInput == "attack ennemy")
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
**displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints)**;
}
Instead of :-
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);

c++ when using functions function call missing argument [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
void Circle::getXYr() {
cout << "(x,y,R) = (" << x << "," << y << "," << r << ")" << endl;
}
double Circle::circArea() {
return (r * r * PI);
}
double Circle::circPeri() {
return (2 * r * PI);
}
void Circle::printCircle() {
cout << "Printing circle " << getXYr << endl;
cout << "Circle Area: " << circArea << endl;
cout << "Circle Perimeter: " << circPeri << endl;
}
I cant use the printCircle function: ERROR Error 1 error C3867: 'Circle::getXYr': function call missing argument list; use '&Circle::getXYr' to create a pointer to member
also the same for the next 2 functions.
It are functions, you have to call them as such. Note the brackets ()
cout << "Printing circle " << this->getXYr() << endl;
cout << "Circle Area: " << this->circArea() << endl;
cout << "Circle Perimeter: " << this->circPeri() << endl;
this-> is not explicitly needed.
Your second error is due this->getXYr() does not return a value. It returns void. There is no basic_ostream overload for it.
You either should put the logic of the getXYr() into the printCircle() function. Or call that function without calling cout on it:
cout << "Printing circle ";
this->getXYr();
cout << "Circle Area: " << this->circArea() << endl;
cout << "Circle Perimeter: " << this->circPeri() << endl;

C++ - How to reset the output stream manipulator flags [duplicate]

This question already has answers here:
Restore the state of std::cout after manipulating it
(9 answers)
Closed 4 years ago.
I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that?
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
I thought this might work:
cout.unsetf(ios::manipulatorname) // Howerver I dont see a manipulator called setfill
Am I on the wrong track?
Have a look at the Boost.IO_State_Savers, providing RAII-style scope guards for the flags of an iostream.
Example:
#include <boost/io/ios_state.hpp>
{
boost::io::ios_all_saver guard(cout); // Saves current flags and format
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
// dtor of guard here restores flags and formats
}
More specialized guards (for only fill, or width, or precision, etc... are also in the library. See the docs for details.
You can use copyfmt to save cout's initial formatting. Once finished with formatted output you can use it again to restore the default settings (including fill character).
{
// save default formatting
ios init(NULL);
init.copyfmt(cout);
// change formatting...
cout << setfill('-') << setw(11) << '-' << " ";
cout << setw(15) << '-' << " ";
cout << setw(11) << '-' << endl;
// restore default formatting
cout.copyfmt(init);
}
You can use the ios::fill() function to set and restore the fill character instead.
http://www.cplusplus.com/reference/iostream/ios/fill/
#include <iostream>
using namespace std;
int main () {
char prev;
cout.width (10);
cout << 40 << endl;
prev = cout.fill ('x');
cout.width (10);
cout << 40 << endl;
cout.fill(prev);
return 0;
}
You can manually change the setfill flag to whatever you need it to be:
float number = 4.5;
cout << setfill('-');
cout << setw(11) << number << endl; // --------4.5
cout << setfill(' ');
cout << setw(11) << number << endl; // 4.5
The null character will reset it back to the original state:
setfill('\0')