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 7 years ago.
Improve this question
my code only saves the last line for eg if i enter 1 abc then press enter and then type 2 def then only 2 def is saved in txt file.
here is my code :-
int main()
{
ofstream rankings;
rankings.open("rankings.txt");
cout << "Enter rank of the Student <space> followed by Name\n"
"Press Ctrl+Z to quit"<< endl;
int rank;
string name;
while (cin >> rank >> name);
{
rankings << rank << ' ' << name << endl;
}
rankings.close();
return 0;
}
You have a superfluous semicolon after your while loop:
while (cin >> rank >> name);
// ^
This will just open a new block in the code afterwards, and leave you with the least values input.
To fix change your loop to
while (cin >> rank >> name) {
rankings << rank << ' ' << name << endl;
}
Related
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 5 years ago.
Improve this question
When i compile and run the program i am getting:The year you were born: 0
Where is the "0" coming from??
Here is the code:
//! Program written by Samer!//
#include <iostream>
using namespace std;
int main()
{
double Year, Age;
cout <<"The year you were born: "<< Year; //!Here the error appears!//
cin >>Year;
while (Year > 2017) //!That't a While loop!//
{
cout <<"Please enter a valid Year:" << Year << endl;
cin >>Year;
}
Age=2017-Year;
cout <<"Your age is:" <<Age;
std::cin.get();
return 0;
}
You are streaming the variable Year:
cout <<"The year you were born: "<< Year;
^^^^
Your code is by design printing Year. This is what you have asked it to do. If you don't want to print it, then don't: cout <<"The year you were born: \n";
Year has not been initialized and using it in this way is undefined behaviour. In this case it seems to print whatever was in the memory at the time of initialization. In your case it happens to print 0.
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 5 years ago.
Improve this question
OK so i know the question is a bit confusing (dont downvote right away, let me explain...)
I have a text file like this:
dim
coins
oponent
I want to read this text file but while reading it, ask the user for specific responses, for example:
"reads line with the word dim" -> asks user the dimensions -> next line -> "reads line with coins" -> asks user how many coins and so forth until the EOF.
Is there anyway to do this? if yes, can you show me how?
Thanks and plz dont downvote, just tell me what's wrong and i will edit the post..
EDIT: This is the way i'm reading the file and asking the user input
void LeitorFich::lerFicheiro(string nomeFich)
{
int i, j, t;
string linha, nome, nome1;
ifstream fich(nomeFich);
while(getline(fich, linha))
{
istringstream iss(linha);
iss >> nome;
if(nome == "dim")
{
cout << nome << " ";
iss >> i >> j;
}
}
cin.get();
fich.close();
}
A simple example will look like this:
Consider I have a file called "test.txt" which contains the very content as yours
string sLine;
ifstream inFile("test.txt");
int Dim1, Dim2, coins, oponent;
while(inFile >> sLine ){
if("dim" == sLine){
std::cout << "Dim1: ";
std::cin >> Dim1;
std::cout << std::endl;
std::cout << "Dim2: ";
std::cin >> Dim2;
std::cout << std::endl;
}
else
if("coins" == sLine){
std::cout << "coins: ";
std::cin >> coins;
}
else
if("oponent" == sLine){
std::cout << "oponent: ";
std::cin >> oponent;
}
}
inFile.close();
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
Here is my first program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
string s;
double d;
while(cin >> a >> s >> d)
cout << a << s << d;
return 0;
}
When I input some simple data and press Enter , the result is shown immediately:
However, the code in another program behaves differently:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double price = 0.0;
void Print();
};
void Sales_data::Print(){//print every record of the Sales_data
cout << "The bookNo of the book is " << bookNo << endl;
cout << "The units_sold of the book is " << units_sold << endl;
cout << "The price of the book is " << price << endl;
}
int main()
{
Sales_data book;
while(cin >> book.bookNo >> book.units_sold >> book.price);
book.Print();
return 0;
}
When I run this code, input some data, and press Enter, it waits for me to input more data rather than show the result.
Could you explain this to me?
Remove the semicolon after the while loop. As it is, it forces the loop to have no body, which means it just cycles over the cin forever. Even better, use braces to delimit the body:
while(cin >> book.bookNo >> book.units_sold >> book.price) {
book.Print();
}
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
I'm new to programming and I've decided to try and make a Calculator that can do stuff other than simple Arithmetic. I have not finished yet, I was just testing to see if it was working so far. As I ran it, and went through Arithmetic by pressing 1 it just stops. Can someone please tell me what Ive done wrong? Thank you.
#include <iostream>
using namespace std;
int main()
{
int frsnum
int secnum
int arithchoice;
int answer;
int x;
cout << "Welcome to the advanced calculator!" << endl;
cout << "What are you trying to calculate: Simple Arithmetic < 1 >" << endl;
cout << " Systems of Equations < 2 >" << endl;
cout << " Matrices < 3 >" << endl;
cin >> x;
if(x == 1)
{
cout << "Add <1>|Subtract <2>|Multiply <3>|Divide <4>";
cin << arithchoice;
}
if(arithchoice == 1)
{
cout << "Whats the first number: "
cin >> frsnum;
cout << "And the second number: "
cin >> secnum;
answer = frsnum + secnum;
cout << "That would be: " answer << endl
}
system("PAUSE");
return 0;
}
The arrows in this statement are incorrect.
cin << arithchoice;
should be replaced by this statement
cin>> arithchoice;
Update
The best way to remember which arrows to use with Cin and Cout is that with when inputing value you are pointing from outside to the computer.
Similarly for cout you throw values from the computer to outside world.
So now if you want to pass values from real world to computer you which arrow will you use >> cin
Similarly for giving results from computer to Real world(user) "<<"
----------------
| |
Real world | <--- computer |
|_______________|
The first thing I've noticed is that in the (x==1) if block, the arrows of the cin are the wrong way round.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a function (below) that checks the user's first name for invalid characters and it works fine.
while(run)
{
size_t positionFirstName = userFirstName.find_first_of(invalidCharacter, 0, sizeof(invalidCharacter));
if (positionFirstName != string::npos)
{
cout << "Please only use letters. Please re-enter your first name." << endl;
cin >> userFirstName;
}
else
{
run = false;
}
}
I also want to check that the user's first name is not shorter than 3 characters.
I have tried a few times, and can get the program to run the first function, but if I put in another function to check name length, it seems to skip it. Any ideas?
Here's a slightly adjusted way to do it:
cout << "Please enter your first name." << endl;
while( cin >> userFirstName )
{
size_t positionFirstName = userFirstName.find_first_of(invalidCharacter, 0, sizeof(invalidCharacter));
if (positionFirstName != string::npos)
{
cout << "Please only use letters.";
}
else if( userFirstName.size() < 3 )
{
cout << "Name must be at least 3 characters long."
}
else {
break;
}
cout << " Please re-enter your first name." << endl;
}
Note that I've avoided repetition, but printing only the errors and handling the input in one place.