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.
Related
So I make an array of string pointers and put a string in at position 0 of the array. If I don't know the length of the string in word[0] how do I find it? How do I then manage that string, because I want to remove the "_." and "." part of the string so I will be left with "apple Tree".How do I resize that string? (functions like strcpy,strlen, or string.end() didn't work, I get errors like "can't convert string to char*" etc)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
int counter=0;
string* word = new string[0];
word[0] = "apple_.Tree.";
return 0;
}
Edit:what i want to do is make a dynamic array of strings(not using vector) and then edit the strings inside
string is a class, so you can use its member functions to manage it. See the documentation.
To remove characters, use std::erase (see this answer).
#include <iostream>
#include <string>
#include <algorithm>
int main() {
// Create array of 10 strings
std::string array[10];
array[0] = "apple_.Tree.";
std::cout << array[0].size() << "\n";
array[0].erase(std::remove(array[0].begin(), array[0].end(), '.'), array[0].end());
array[0].erase(std::remove(array[0].begin(), array[0].end(), '_'), array[0].end());
std::cout << array[0];
return 0;
}
so the task for this code is to copy students' names and grades from an csv excel sheet into xcode and then put them into arrays and put them into a new excel sheet. The problem that i seem to be having is that the getline does not go to the next line. To make sure that there wasn't an error somewhere in this code that would cause that to happen, I wrote a very small and completely different program to see how getline works and found that it does not skip to the next line. In fact, if I raise the character amount to a high number, it will just copy in the entire excel info into the array. Here's my code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
char line[80];
string students[100];
int grades[50][20];
char *p;
int r;
int q;
void read_sheet();
void print_sheet();
int main() {
read_sheet();
print_sheet();
return 0;
}
void read_sheet(){
ifstream file1("/Users/JohnnyD/Downloads/Project_MAC101.csv");
file1.getline(line, 79); // this puts everything from the first line of the
// excel sheet into the array line
for(r=0;r<50||(!file1.eof());r++){ // this is a loop that goes up to
// either the amount of students
//(50 is max) or the end of the
file1.getline(line, 79); // this is suppose to put everything
//from the second line into the array
// line, but I don't think it is doing
// that.
p=strtok(line,","); // this takes everything from the first
// line that is before the comma and
//puts it into p.(should be only a single
// student's name
students[r]=p; // this puts the name into the array
// called students
cout << students<<endl; // this is only a test to see if the names
// are going properly to the array. I
// wouldn't normally have this in the code.
// This is where I found out that it's not
// skipping to the next line because the
// output just spits out "name" over and
// over again which means that it never got
// passed the first word in the excel sheet.
// ("name" is the first word in the first
// line in the excel sheet)
for(q=0;q<20;q++){ // this is a loop that goes to the end of
// the column where 20 is the max amount
// of grades
p=strtok(NULL,","); // puts each grade before the comma into p.
if(p==NULL) // if it's the end of the line, break out
break; //of the loop.
grades[r][q]=atoi(p); // this changes the string to integer and then
// puts it into the array grades
}
}
file1.close();
}
void print_sheet(){
ofstream file2("testing.csv");
for(int y=0;y<=r;y++){
file2<<students[y];
for(int h=0;h<q;h++){
file2<<grades[y][h];
}
file2<<endl;
}
file2.close();
}
This is the code that I used to test to see if getline was actually moving to the next line.
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
char line[80];
int main() {
ifstream file1("/Users/JohnnyD/Downloads/Project_MAC101.csv");
file1.getline(line, 79);
cout << line<<endl;
file1.getline(line, 79); // shouldn't this then go to the next line?
cout << line<<endl; // It doesn't. It only repeats the first getline
return 0;
}
The usual idiom for reading from a file is to use a while statement.
In your case, you can limit it with another variable:
const unsigned int maximum_records = 50U;
unsigned int record_count = 0U;
std::string text_line;
// ...
while (getline(datafile, text_line) && (record_count < maximum_records))
{
//...
++record_count;
}
If either the file operation failed or the maximum records have been reached, the input session will terminate.
I have an array that holds strings. I was wondering if there's any way to add X amount of chars to the string in the array.
For example if a user inputs the number 10 and then the letter A, I want stringarray[x] to have the value of AAAAAAAAAA.
At the moment I am using a for-loop but I was wondering if there is an easier and more efficient way of doing this. One that doesn't require a loop.
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
#include <fstream>
using namespace std;
int main(){
char letter;
int number;
string stringarray[5] = {" "};
cin >> letter; // letter to add
cin >> number; // number of times
cout << stringarray[1]; // here I want the result to be letter x number
return 0;
}
I can only use these libraries.
I don't think it's necessary to post my for-loop since it already works. I am only wondering if there's any way to do it without the loop.
A C++ way of doing this is to use std::string s(10, 'A'); and get const char *stringarray = s.c_str() from it if you need a const char *.
#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;
}
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.