C++ Program help: Structs, arrays, loops, input files, loop not working? - c++

I am trying to write a program which will declare an array of 5 structs from information read from a file. Then I use a loop to the print the information of every element in the array.
The code I have written only seems to read one line from the txt. file. Any tips or advice would be appreciated.
#include <istream>
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
struct Bankinfo{
string name;
int accountnum;
float checking;
float savings;
string phone;
} bankinfo[5];
int i;
i=0;
cout<<"This is a test program"<<endl;
char x;
x=0;
for (i=0;i<=6;i++)
{
ifstream infile;
char testinfo [10001];
infile.open("testinfo.txt");
cin.get(testinfo,10001);
cout<<testinfo<<endl;
infile>>bankinfo [i].name>>bankinfo [i].accountnum>>bankinfo [i].checking>>bankinfo [i].savings>>bankinfo [i].phone;
cout<<setw(10) << (bankinfo[i].name);
cout<<setw(10) <<(bankinfo [i].accountnum);
cout<<setw(10) <<(bankinfo [i].checking);
cout<<setw(10) <<setprecision (2)<<fixed<<(bankinfo [i].savings);
cout<<setw(15) <<(bankinfo [i].phone);
}
cout<<" "<<endl;
cout<<"Thanks for using the program"<<endl;
return (0);
}

You're opening the file in each iteration of the loop in i. Try to get out of the loop the infile.open(...). Now it will read more lines. I don't see the purpose of that cin.get(...) either.

Related

Getting Information from input file C++

I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween

Executable has error during runtime

I have the following code which compiles properly:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "INVESTMENT.h"
#include <vector>
using namespace std;
int main()
{
ifstream inputFile("jroth.csv");
string sHolder; //used as placeholder for string
float fHolder; //used as placeholder for float
double dHolder; ///used as placeholder for double
// while (inputFile.good())
vector<int> InvestVector; //will hold class (Investment) which contains string, double, and float
for (int i=0; i <= 7; i++)
{
Investment InvestVector[i]; //create new class for each line being pulled from .csv file
getline(inputFile, sHolder, ','); //pull in investment symbol as string
InvestVector[i].setSymbol(sHolder); //store string in the class
cout << InvestVector[i].getSymbol(); //verify string store properly
}
return 0;
}
As soon as the program runs, the executable crashes. Any thoughts on why?
Assuming you are using a C++ compiler with variable-length arrays, which is not standard C++, then this line:
Investment InvestVector[i];
creates an array with "i" places, numbered from 0 to i-1. Then these two lines:
InvestVector[i].setSymbol(sHolder);
cout << InvestVector[i].getSymbol();
will try to work with position number "i" of the array, that is, one past the end of the array.

Not Fully read from the text file

I am trying to read some integer numbers from a text file in C++. It is weird problem that it reads only 13 numbers , but my file contains 25 numbers . I searched but not found any thing , someone suggested to add ios::binary , but not working.
Why ??
here is this part of code.
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
ifstream myfile;
myfile.open("Nvector.txt");
vector<int> N;
for(int j=0; j<25; j++)
{
int input;
myfile>> input;
N.push_back (input);
}
system("PAUSE");
return 0;
}
You might want to check the text file that you're reading from, there can be special end of line characters that will cause the integers to be concatenated/removed from the vector, there might also be problems related to the vector

Getline only printing out last line when called multiple times

So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}

the output is not what i typed.It is --> (畳慨汩朠灵慴찀쳌쳌쳌)

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
char x[20];
cout << "enter something\n";
cin.getline(x,20);
ofstream o("d:/tester.txt");
//o.write( (char*)&x , sizeof(x) );
for(int i = 0 ; i<=19 ; i++ ) {
o.put(x[i]);
}
}
I am not getting that output in the file the one which i enter during program . for eg. the output is 畳慨汩朠灵慴찀쳌쳌쳌 on writing suhail gupta.
What is the problem with the code ? Even when i use o.write( (char*)&x , sizeof(x) ); (the commented statement) i get the same output.
What is the reason?
Your program involves undefined behavior. The x array is not fully initialized and you read from the uninitialized indices. Besides, you always write 20 bytes, independent of what you read from the user.
I guess you use some text editor like Notepad. The latter has bugs when trying to guess the encoding. It appears that it guesses the file is UTF16 and displays 20/2 == 10 characters instead.
To solve the problem, store to the file exactly the number of characters entered by the user. Use std::string to make it easier.
Edit: The C++ way:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string x;
cout << "enter something\n";
getline(cin, x);
ofstream o("d:/tester.txt");
o << x;
}