-1

Data in the input file:

Wilson Jack 87236.45 11

My code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
     ofstream out;
     ifstream in;

string Lastname, Firstname;
double salary;
int increase;

in.open("Lab5_Ex_3_Input.txt");
out.open("Lab5_Ex_3_Output.txt");



in >> Lastname >> Firstname >> salary >> increase;
out << "Lastname: "<< Lastname << "Firstname " << Firstname << "salry :" << salary <<"increase: "<< increase <<endl;




in.close();
out.close();

return 0;
}

So, when I check the output file I am getting:

Lastname: Firstname salry :-9.25596e+061increase: -858993460

what am I doing incorrectly?

Ninjaboi
  • 3
  • 4
  • @EdHeal: SO did post the content of input file. – user3813674 Feb 06 '16 at 08:00
  • 1
    This works for me. How did you compile that? – user3813674 Feb 06 '16 at 08:02
  • 1
    Five bucks says the program is not being run from the same directory as the input file. IDEs sometimes play silly little tricks like that. – user4581301 Feb 06 '16 at 08:44
  • If you are using GNU C++ it should work if this is the only line in your input text. If you have errors in your input text then your code needs more error handling to handle different input types/missing columns etc. What Compiler are you using ? Compiler, Platform/OS etc. ? – gusaki Feb 06 '16 at 09:22
  • user4581301 nope double checked the directory of in file it is in the right place, i wrote another program with another in file in it(it had just numbers) that worked just fine. @linuxmonk i am using Mcrsft visual studios 2013, on windows 10. Hmm that is not the only line in input text though there are 3 more lines in the in file with identitical string and values. I thought since i am only reading the first line it didn't matter what was in other lines, do i need to include that? – Ninjaboi Feb 06 '16 at 15:54
  • @Ninjaboi Can you try flusing your streams for read and write. I am really surprised how the strings are not being read. Also, was your file created on the Windows machine or does it have Unix style EOLs ? – gusaki Feb 06 '16 at 16:40
  • @linuxmonk The file was created on windows and sorry but how do i flush ? – Ninjaboi Feb 06 '16 at 17:22
  • @Ninjaboi - http://www.cplusplus.com/reference/ostream/flush-free/. You need to check input stream internal state as well after your read. You must study a bit more about streams before debugging this code properly. Unless there is a silly error related to wrong file being processed etc. Which I hope you have been careful. – gusaki Feb 07 '16 at 17:50

2 Answers2

1

Try this:

if (!(cin >> value >> value2 >> value3)) {
    cout << "input failed" << endl;
    return -1;
}

My guess is that your input fails. You could also check if the file was opened correctly at all, which your code is missing.

BTW: There's no need to explicitly close the streams, they are closed automatically when they go out of scope and their destructor is called.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • you are right, I do get "Input failed" when i plug that in, so what should i do? – Ninjaboi Feb 06 '16 at 18:35
  • Did you heed the suggestion to "check if the file was opened"? Note that I consider you mature enough to find out how, which is why I didn't give any details how to do that. – Ulrich Eckhardt Feb 07 '16 at 08:19
0

Your program is likely having trouble reading the input file. Why? Based on the output values:

Lastname: Firstname salry :-9.25596e+061increase: -858993460

This is due to the fact that both Lastname and Firstname are empty (i.e. there is nothing after the colon), that the numbers following salry and increase is common when you've uninitialized your variables as you've done in your code.

What you should do is check to see if the file is open:

if (!in.is_open()) {
    std::cerr << "Error opening input file!\n";
    exit(1);
}
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • @jdr1 hey thanks for the reply, unfortunately i can not use if statements, because if statements aren't included in the chapter and i will get marked down for not using the correct method :( – Ninjaboi Feb 06 '16 at 15:57