Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I currently have this code and all of the formatting is correct... I just can't seem to get it to create rows in the output text file... How would I do this? I've tried to do a for loop in the function and in the main() but it seems to not work if I do that so I am very confused right now.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void output(string flightnumber, int arrival1, int arrival2, int realarrival1, int realarrival2, char dummy1, char dummy2)
{
ofstream flight;
flight.open("flightdata.dat");
if (flight.fail())
{
cout << "Error..." << endl;
exit(1);
}
cout << "Enter the flight number: ";
cin >> flightnumber;
if (flightnumber == "end")
{
exit(1);
}
flight << flightnumber << setw(4);
cout << "Enter the scheduled/actual arrival times (hh:mm hh:mm):";
cin >> arrival1 >> dummy1 >> arrival2 >> realarrival1 >> dummy2 >> realarrival2;
flight << arrival1 << dummy1 << arrival2 << setw(4) << realarrival1 << dummy2 << realarrival2;
flight << ('\n');
}
int main()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
output(flightnumber, arrival1, arrival2, realarrival1, realarrival2, dummy1, dummy2);
return 0;
}
You are not appending your file whenever you write it truncates and creates a new file, add appending flag and open your file as.
flight.open("flightdata.dat", ios_base::app);
You are using uninitialized variables in main. They serve no purpose in there anyway. Remove variable declarations from main and put them in output:
void output()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
ofstream flight;
flight.open("c:\\test\\___flightdata.txt", ios::app);
...
}
int main()
{
output();
return 0;
}
You may want to add ios::app flag as pointed out in the other answer.
Related
This question already has answers here:
How do I pass a cin'd c style string to a function?
(2 answers)
Closed last year.
Here is my code I am expecting the output but I am not getting .It stop after taking the input
I am expecting the output if i give name Harsh
Your name is Harsh
#include <iostream>
#include <cstring>
using namespace std;
int main() {
cout << "Enter your name" << endl;
char *s;
cin >> s;
cout << "Your name is " << s;
return 0;
}
I have also tried with cin.getline(s,100);but still it is not working.
So I request to you to solve the problem and give me solution.
Your code has undefined behavior because you are not allocating any memory for s to point at. s is an uninitialized pointer.
Try this instead:
#include <iostream>
using namespace std;
int main(){
cout << "Enter your name" << endl;
char s[100];
cin >> s; // or: cin.getline(s,100);
cout << "Your name is " << s;
return 0;
}
Alternatively, you should use std::string instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "Enter your name" << endl;
string s;
cin >> s; // or: getline(cin,s);
cout << "Your name is " << s;
return 0;
}
s in your code is unallocated.
Since it is C++ we're talking about, you probably don't want to use pointers and memory allocation, and use std::string instead.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout << "Enter your name" << endl;
string s; // Instead of dealing with char* allocation and memory issues.
cin >> s;
cout << "Your name is " << s;
return 0;
}
you have done it correctly but the problem with output is because of the memory allocation.
You have to allocate memory and try to avoid the concept of a pointer in that. Instead
Use string s;
or
char s[50];
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I am beginning to code with C++. This is what I want my program to do:
1. The user inputs the number of persons he wants to write into the database (a simple txt file).
2. The user inputs the name of the fist person (Nombre_usuario), the age(edad) and the ID (C.I:).
3. The user repeats the same process until he reach the number he input on step 1.
This is my code:
#include<iostream>
#include<windows.h>
#include <direct.h>
#include <fstream>
using namespace std;
int main(){
string Nombre_usuario;
int edad;
int cerdula;
int num;
//----------LOOP----------
std::cout<<"¿Cuántos clientes desea ingresar ahora?:";
std::cin>>num;
for(int x=0; x<num;x++){
//-------------User Interface------------
std::cout<<"Ingrese su nombre y apellido:";
std::getline(cin, Nombre_usuario);
std::cout<<"Ingrese su edad:";
std::cin>>edad;
std::cout<<"Ingrese su C.I.:";
std::cin>>cerdula;
}
//----------------Data storage on txt-------------------------------
_mkdir("C:\\FP");
std::ofstream outfile;
outfile.open("base_de_datos_cli.txt", std::ofstream::out, std::ofstream::app);
outfile<<"Nombre de usuario:"<<Nombre_usuario<<std::endl;
outfile<<"Edad:"<<edad<<std::endl;
outfile<<"C.I.:"<<cerdula<<std::endl;
outfile.close();
return 0;
}
The result, however, looks like this:
So, the input fields are mixed up, it does not repeat the process the number of times I ask it to, etc...
Why is this happening?
Thank you very much.
It does not repeat the process the number of times I ask it to
The chunk of code that outputs the data to "base_de_datos_cli.txt" should be inside the loop, since every time you get a new input, the last input is lost.
The input fields are mixed up
Read this. You can't use std::getline after another input without calling cin.ignore() to clear the extra newline character left in the input stream.
Here is a new version of your program that works and is better organizated. I tried to keep the code in Spanish as much as possible. Please do not make a straight copy, but try to understand why your code wasn't working:
#include <iostream>
#include <direct.h>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
_mkdir("C:\\FP");
std::ofstream outfile;
outfile.open("C:\\FP\\base_de_datos_cli.txt", std::ofstream::out | std::ofstream::app);
string Nombre_usuario;
int edad;
int cerdula;
int num;
std::cout << "¿Cuántos clientes desea ingresar ahora?:";
std::cin >> num;
for (int x = 0; x < num; x++) {
std::cout << "Ingrese su nombre y apellido:";
std::cin.ignore();
std::getline(std::cin, Nombre_usuario);
std::cout << "Ingrese su edad:";
std::cin >> edad;
std::cout << "Ingrese su C.I.:";
std::cin >> cerdula;
outfile << "Nombre de usuario:" << Nombre_usuario << std::endl;
outfile << "Edad:" << edad << std::endl;
outfile << "C.I.:" << cerdula << std::endl;
}
outfile.close();
return 0;
}
I've read that getchar() should actually be preferred over _getch() ( _getch() also needs including conio.h ). However, if I use getchar() in this sample program...
#include <iostream>
#include <conio.h>
using namespace std;
class student {
int id;
int marks;
public:
void getdata() {
cout << "\n Enter id";
cin >> id;
cout << "\n Enter marks";
cin >> marks;
}
void putdata() {
cout << "\n" << id << "\t" << marks;
}
};
int main() {
student tom;
tom.getdata();
tom.putdata();
getchar(); // vs. _getch();
return 0;
}
..then using getchar() won't wait for the input of a character to prevent the console window from being closed too early, which _getch() does.
Does anyone know the reason for this? And should getchar() really be preferred over _getch()?
BTW, I'm using MS Visual Studio 2015.
Thanks in advance and kind regards
EDIT: I'd consider my question as not being a duplicate actually, since I was wondering about the reason for the different behavior, which had not been answered under "Is there a decent wait function in C++?", and which got clarified now.
I new to programming and was trying to implement struct program in c++, it's simple program but it's not printing proper result. please tell me why?
#include <iostream>
using namespace std;
struct classroom {
int number;
char name[9];
int marks;
void getAndPrint()
{
cout << "struct classroom ";
cin >> number;
cout << number << '\n';
cin.get(name, 9);
//cin>>name;
cout << name;
cin >> marks;
cout << marks;
}
};
int main()
{
classroom room1;
room1.getAndPrint();
int i;
cin >> i;
return 0;
}
In function getAndPrint() I'm using cin.get()..the compiler execute the properly till printing the "number" but when it comes on cin.get(name,9) it print garbage and skips the rest of the code inside the funcion. If i use cin>>name then it's working properly.
Can anyone tell what exactly is the problem?
first, in C++, struct is class with access_modifier is public.
second, you should try read:
Difference between cin and cin.get() for char array
The structure definition does not contain such a function like see
room1.see();
^^^^
I think you mean
room1.getAndPrint();
Also before this statement
cin.get(name, 9);
insert at least this statement
cin.ignore();
Or you even can include the header <limits> and insert statement
#include <limits>
//...
cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm trying to read individual lines to different variables and I'm having trouble having it read anything past the first line
ifstream inputFileStream;
inputFileStream.open( fileName );
if (inStream.good() && inStream.open())
{
string empNum;
string name;
string streetAddress;
getline(inStream, empNum);
getline(inStream, name);
getline(inStream, streetAddress);
cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;
}
Also the txt file looks like this:
12
Bob
123 Main
555-555
This code works for me. copy & paste & run (without changes...)
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream inputFileStream("text.txt");
if (inputFileStream.good() )
{
string empNum;
string name;
string streetAddress;
getline(inputFileStream, empNum);
getline(inputFileStream, name);
getline(inputFileStream, streetAddress);
cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;
}
return 0;
}