iostream and structure in cpp

I must write a cpp program to read contents of the file student.txt and produce the following output as shown below. Make sure your output must be the same as the sample output in student.txt.



student.txt

Student Id = 1200233
Name = Matt
Course = CS
Phone Number = 790-3233

Student Id = 1201237
Name = Benny
Course = IA
Phone Number = 432-4579

Student Id = 1300899
Name = Salma
Course = IB
Phone Number = 790-0000

Student Id = 1502378
Name = Ken
Course = CN
Phone Number = 892-3765

Student Id = 1603679
Name = Sammy
Course = IB
Phone Number = 693-2367



This is my code and i can't run my code. Please help. Thank you.

#include <iostream>
#include <fstream>
#include <cstring>

struct Student
{
int ID;
char name[20];
char course[3];
char phone[10];
};
using namespace std;

int main()
{
Student s;
char p[256];
int i;
ifstream in;
in.open("student.txt");
if (!in)
cout << "\n\n Cannot open file student.txt." << endl;
else
{
for (int i = 1; i <= 6; i++)
in >> p;
in >> s.ID, 256;
in >> s.name;
in >> s.course;
in >> s.phone;
}

in.close();
cout << " " << endl;

system("pause");
return 0;
}


i can't run my code

What does that mean?

Does it not compile?

Does it compile, but not run?

Does it run, but crash?

Does it run, but behave in some unexpected way?

We're not mind-readers. If you have a problem, tell us clearly and precisely what the problem is.

it can compile but i can't see any output.
programmer35 wrote:
it can compile but i can't see any output.

Assuming it read the input file then your only output is
cout << " " << endl;
A space isn't all that visible.


Admittedly, you are also trying to read 6 items from a file that appears to only contain 5.


And your for loop is precisely this long:
1
2
for (int i = 1; i <= 6; i++)
   in >> p;              // ends HERE without braces 



This line might have a problem (or two):
in >> s.ID, 256;


You would find life easier with std::string than c-strings.


I suspect you also need more than one student.


Please use code tags.
Last edited on
s should be an array, as best as I can guess, if you want multiple students (?).
in >> s[index].ID; //etc
I have done some changes on my code.



#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student
{
int ID;
char name[20];
char course[3];
char phone[10];
};

void readDetails()
{
Student s;
}


int main()
{
Student s[5];
ifstream in;
int num;
in.open("student.txt");
if (!in)

cout << "\n\n Cannot open file student.txt." << endl;
else

in >> num;
for (int i = 0; i < num; i++)
in >> s[i].ID >> s[i].name >> s[i].course >> s[i].phone;
for (int i = 0; i < num; i++)
std::cout << s[i].ID << " " << s[i].name << " " << s[i].course << " " << s[i].phone << endl;

in.close();
cin.ignore();
cin.get();
system("pause");
return 0;
}

void readDetails()
{
int details, i;
ifstream in ("student.txt");
readDetails >> details;
for (i = 0; i < details; i++) {
cin >> s.ID;
cin >> s.name;
cin >> s.course;
cin >> s.phone;

}





I have to start with this question:

Is this the ACTUAL content of student.txt?

Student Id = 1200233
Name = Matt
Course = CS
Phone Number = 790-3233

Student Id = 1201237
Name = Benny
Course = IA
Phone Number = 432-4579

Student Id = 1300899
Name = Salma
Course = IB
Phone Number = 790-0000

Student Id = 1502378
Name = Ken
Course = CN
Phone Number = 892-3765

Student Id = 1603679
Name = Sammy
Course = IB
Phone Number = 693-2367


If it REALLY is the ACTUAL content, then the line in your most recent code:

in >> num;

Has no "number" to pull in. It would have to be a "5" in that text in order for it to be possible to read that in.

Files of this type do have two basic forms...they might have a definite number at the top. That's a kind of header information, stuff that tells you about the file - in images you may already know that as meta-data.

The other form is just a stream. There is no header, just item after item. For such a file the plan must be to read until there's nothing left to read.

The code must account for that.

So, is the actual file different?

Further, if this is the actual file, what is the plan to deal with the title information?

Consider, this line from the file:

Student Id = 1200233


Think. What would come from a read of this line where code says this:

cin >> s;

Where "s" is some kind of string or character array. Do you expect to get the student id number? Do you expect to get the word "Student"?


Last edited on
This sounds an awful lot like homework. Also please use code tags, and try using scanf() with freopen() on stdin
This sounds an awful lot like homework.


I can't imagine it being anything else
Topic archived. No new replies allowed.