Question

my code deosn't run and it doesn't show any error. what's wrong about it !! it...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
my code deosn't run and it doesn't show any error. what's wrong about it !! it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In c++ please How do I get my printVector function to actually print the vector out?...

    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...

    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...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    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>...

    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];...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    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...

    #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...

    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

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT