my code deosn't run and it doesn't show any error. what's wrong about it !! it should loads data from a text file into an array of structs and prints the array to the screen
here is the code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Company {
string Departmentname;
int Departmentnum;
};
const int MAX = 20;
int main() {
ifstream File;
Company arrayofdepartment[MAX];
int Departmentcount ;
File.open("comapny.txt");
if (File)
{
string theDepartmentname;
int theDepartmentnum;
Departmentcount = 0;
while (File >>
theDepartmentname >> theDepartmentnum)
{
if
(Departmentcount < MAX)
{
arrayofdepartment[Departmentcount].Departmentname =
theDepartmentname;
arrayofdepartment[Departmentcount].Departmentnum
= theDepartmentnum;
Departmentcount++;
}
else
{
cout << "Capacity is reached. No more new
student"
<< " can be handled"
<< endl;
break;
}
}
for (int i = 0; i
<Departmentcount; i++)
{
cout <<
arrayofdepartment[i].Departmentname << ",";
cout <<
arrayofdepartment[i].Departmentnum << endl;
}
}
else
{
cout << "Error opening the
text file" << endl;
}
return 0;
}
/////////////////////////////////////////////////////////////////////
name of file:
company.txt
content of the file as following:
Management,1063
Production,5023
Human Resources,1077
Marketing,5231
Code:
//include vector and sstream
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
struct Company {
string Departmentname;
int Departmentnum;
};
const int MAX = 20;
int main() {
ifstream File;
Company arrayofdepartment[MAX];
int Departmentcount ;
File.open("company.txt");
if (File)
{
string
theDepartmentname;
int theDepartmentnum;
Departmentcount = 0;
/*start of changes*/
vector<string> v;
//string to store current
line.
string temp;
while (getline(File,temp))
{
std::stringstream ss(temp);
std::string item;
while
(getline(ss, item, ','))
{
v.push_back(item);
}
//assigning splitted string to both the variables.
theDepartmentname = v[0];
//converting string to integer.
stringstream s(v[1]);
s>>theDepartmentnum;
//cleaing the vector.
v.clear();
/*end of changes*/
if
(Departmentcount < MAX)
{
arrayofdepartment[Departmentcount].Departmentname =
theDepartmentname;
arrayofdepartment[Departmentcount].Departmentnum =
theDepartmentnum;
Departmentcount++;
}
else
{
cout << "Capacity is reached. No more new student"
<< " can be handled" << endl;
break;
}
}
for (int i = 0; i
<Departmentcount; i++)
{
cout
<< arrayofdepartment[i].Departmentname << ",";
cout
<< arrayofdepartment[i].Departmentnum << endl;
}
}
else
{
cout << "Error opening
the text file" << endl;
}
return
0;
}
Output:

Hope your problem solved.
Feel free to ask doubts in comment section.
my code deosn't run and it doesn't show any error. what's wrong about it !! it...
In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){ ifile.open(filename.c_str(), ios::in); if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); } } void...
I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...
I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password) I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a...
The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
CODE ERROR Please help to fix the error. I don't know why I can not output to file. when I open the output file there is an error. here is the input data: Princeton University NJ Princeton 41820 8014 0.0740 0.98 0.97 Harvard University MA Cambridge 43838 19882 0.0580 0.97 0.97 Yale University CT New Haven 45800 12109 0.0690 0.99 0.98 Columbia University NY New York 51008 23606 0.0690 0.99 0.96 Stanford University CA...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...
My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer { int accountNumber; string customerFullName; string customerEmail; double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) { cout << fixed << setprecision(2); for (int i = 0; i...
#include #include #include #include #include #include #include using namespace std; const int MAX = 26; string addRandomString(int n) { char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; string res = ""; for (int i = 0; i < n; i++) res = res + alphabet[rand() % MAX]; return res;...
1. Suppose the file mystery.txt contains the text below: The whole thing starts about twelve, fourteen or seventeen. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string word; int i 0; ifstream read("mystery.txt"); while(!read.eof()) { read>>word; if(i < word. length()) cout<<word[i]; i++; } cout<< endl; } seventeen teen