How to cout with input in c++? [closed] - c++

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 4 years ago.
Improve this question
Hello guys actually i want to show output with input
e.g:
int main()
int a, b, c;
cin >>a >>b;
c = a-b;
cout <<"Here would be input e.g: 4+5=" << c <<endl;
cout <<"Here would be input e.g: 4-5=" << e <<endl;
If input is in this format a+b and user enter input's then output show after input like this 4+5= and after equal answer would there. Thanx in advance

cout << "Here would be input e.g: " << a << "+" << b<<"=" << c << endl;
cout << "Here would be input e.g: " << a << "-" << b<<"=" << e << endl;

Add the input variables in the cout command as follows:
cout << a << " + " << b<<" = " << c << endl;

Related

Why std::endl can't be used after std::cin [closed]

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 10 months ago.
Improve this question
What's the diff between \n and endl? Is \n faster?
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter 2 numbers:" << endl;
int a = 0, b = 0;
cin >> a >> b;
cout << "The sum of " << a << " and " << b << " is " << a + b << endl;
return 0;
}
Yes, '\n' is faster here. std::endl will additionally call flush() which is not needed in this case.

C++ outputting weird [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 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.

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.

for-loop construction start in a c++ program [closed]

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 9 years ago.
Improve this question
I have a problem in the for-construction, I don't know why my counting starts from 0 but you can't write on it(you can write starting with Register 1).I've marked in the code where is the problem.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string day, date;
int registerCount;
std::cout << "INPUT DATA"
<< std::endl << std::endl
<< "Enter the day in which you want to perform the register: "
<< std::endl;
std::cin >> day;
std::cout << "DATE:" << std::endl;
std::cin >> date;
std::cout << "Enter the number of registers you wanna perfom for the day "
<< day << ":" << std::endl;
std::cin >> registerCount;
std::vector<std::string> registers(registerCount);
for (int i = 0; i < registerCount; ++i) **here is the problem**
{
std::cout << "Register " << i << ":" << std::endl; **it starts from 0 but you can't write on Register 0**
std::getline(std::cin, registers[i]);
}
std::cout << "The data for the day of " << day << " are the following: "
<< std::endl;
std::cout << "DATE: " << date << std::endl;
for (int i = 0; i < registerCount; ++i)
std::cout << registers[i] << std::endl;
}
Use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Header - #include<limits>
after your
std::cin >> registerCount;
to "eat up" the trailing newline coming from previous std::cin which is cause of skipping of the first input
for (int i = 0; i < registerCount; ++i)
You're defining i as starting at 0 here. Very simply, all you need to do is change the "i = 0" to "i = 1".
The way you're assigning to that vector looks a little bit wrong too. I think you need to use push_back() on the vector in that syntax, but I'm not too familiar with all the different operators for vector.

geany will not let me use the "endl;" when using c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
every time I try to compile it I get an error message along the lines of
"exercise3.cpp:10:49: error: expected ‘;’ before ‘endl’"
and if I add the ; before endl I get the error message
"exercise3.cpp:8:71: error: statement cannot resolve address of overloaded function"
I have no idea what the problem is with this, so any help would be appreciated.
code: (sorry about not being lined right but i assure you that it is.)
#include "iostream"
using namespace std;
int main()
{
cout << "hello there!\n";
cout << "here is 5: "<< 5 << "\n";
cout << "the manipulator end1 wrotes a new line to the screen."; endl;
cout << "here is a very bug number:\t" << 70000 endl;
cout << "here is the sum of 8 and 5:\t" << 8+5 endl;
cout << "heres a fraction:\t\t" << (float) 5/8 endl;
cout << "and a very big number:\t" << (double) 70000*70000 endl;
cout << "you really dont have to type this line XD\n";
cout << "or this one :P cause its not really needed!\n";
return 0;
}
You need to insert << before endl.
cout << "the manipulator end1 wrotes a new line to the screen."; endl;
Should be
cout << "the manipulator end1 wrotes a new line to the screen." << endl;
And so on
You need << before each endl.