i hava a data

student id examcode
1 002
006
001
2 123
589

How to store data when it have two data in 1 foleslike above? in .txt ot .cpp?

and how can c++ called the data?

Recommended Answers

All 73 Replies

In .txt , read the text file using fstream

but how can the c++ detectthat the data is student id and examcode?

but how can the c++ detectthat the data is student id and examcode?

Please take care in your posts. detectthat? foleslike?

C++ cannot determine what type of data it sees. You have to determine what the data differences are and program the code to understand the difference.

It can't. You have to tell it what it is. As you are reading it in from the text file, you know the text file's layout. You know when a student id is coming, so when it does, you read the data in to the appropriate variable which you have intentionally defined to take that type of data. Ditto with the exam code.

Are you sure about that input file? It looks like some lines have two pieces of input and some have one.

Something like this (assuming your ifstream is called "ins"):

int studentid;
int examcode;

ins >> studentid;
ins >> examcode;

You know studentid comes before examcode, so read it in first. Not sure if there is one exam code per student id or if there are three. Hopefully you know the layout of the input file ahead of time and can accomodate your C++ program to it. Change the above code as needed if you want to do an array of structs or something, which you may end up with. This was just to point out how you can read something in if you know the order ahead of time.

but how to save a data? int .txt? or c++? how to determine the student id and exam code in a data?

student id examcode
1 001
002
003
2 005
009
004

#include<iostream>
#include<fstream>
using namespace std;
 
int main()
{
            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
{
            cout << "While opening a file an error is encountered" << endl;
}
            else
{
            cout << "File is successfully opened" << endl;
}
           while(!stream1.eof())
{
           stream1 >> a;
           cout << a << endl;
}
           return(0);
 
}

I do like this, but how can the programming detect the data which one is student id and examcode?

Well, it can't when you do it that way because again, you have not specified in your C++ program a way to store these different variables. It looks to me like there are three exam codes for every student id. The student id comes first, then the three exam codes, in the file. So if you set up a struct like this:

struct student
{
     int studentid;
     int examcode1;
     int examcode2;
     int examcode3;
};

Keeping your code outline, you can do something like this:

student aStudent;

while(!stream1.eof())
{
      stream1 >> aStudent.studentid;
      stream1 >> aStudent.examcode1;
      stream1 >> aStudent.examcode2;
      stream1 >> aStudent.examcode3;

      cout << aStudent.studentid << endl;
      cout << aStudent.examcode1 << endl;
      cout << aStudent.examcode2 << endl;
      cout << aStudent.examcode3 << endl;
}

Everything is in the right place since you know ahead of time the order of the data in the file and you program the C++ program to correspond with that.

#include<iostream>
#include<fstream>
using namespace std;

int main()

     struct student;
{
     int studentid;
     int examcode1;
     int examcode2;
     int examcode3;
}

{
            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
{
            cout << "While opening a file an error is encountered" << endl;
}
            else
{
            cout << "File is successfully opened" << endl;
}
            student aStudent;
            while(!stream1.eof())
{
            stream1 >> aStudent.studentid;
            stream1 >> aStudent.examcode1;
            stream1 >> aStudent.examcode2;
            stream1 >> aStudent.examcode3;

            cout << aStudent.studentid << endl;
            cout << aStudent.examcode1 << endl;
            cout << aStudent.examcode2 << endl;
            cout << aStudent.examcode3 << endl;    
	  
}
            return(0);

}

i cant open tha file....dont know how to solve

#include<iostream>
#include<fstream>
using namespace std;
#include "1.h"

 
int main()
{
            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
{
            cout << "While opening a file an error is encountered" << endl;
}
            else
{
            cout << "File is successfully opened" << endl;
}
            student aStudent;   
	     	while(!stream1.eof())
{
            stream1 >> aStudent.studentid;
            stream1 >> aStudent.examcode1;
			stream1 >> aStudent.examcode2;
			stream1 >> aStudent.examcode3;
			stream1 >> aStudent.examcode4;
			stream1 >> aStudent.examcode5;
			stream1 >> aStudent.examcode6;
			stream1 >> aStudent.examcode7;
			stream1 >> aStudent.examcode8;
			stream1 >> aStudent.examcode9;
			stream1 >> aStudent.examcode10;
			stream1 >> aStudent.examcode11;

			cout << aStudent.studentid << endl;
			cout << aStudent.examcode1 << endl; 
			cout << aStudent.examcode2 << endl;
			cout << aStudent.examcode3 << endl;
			cout << aStudent.examcode4 << endl; 
			cout << aStudent.examcode5 << endl;
			cout << aStudent.examcode6 << endl;
			cout << aStudent.examcode7 << endl; 
			cout << aStudent.examcode8 << endl;
			cout << aStudent.examcode9 << endl;
			cout << aStudent.examcode10 << endl;
			cout << aStudent.examcode11 << endl;
}
           return(0);
 
}

but the output is like this...........

-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
<snipped a lot more of the same>

1 001
002
003
2 005
009
004

If you look at the data you will see that the student id is just a single character, but grades are all 3 digits left-padded with 0s. So if you read the file in as words (strings) instead of integers you can easily test the string to see if the word is three characters then its a grade, otherwise its an ID.

read a word
is the length of the word 3 characters ?
yes, then we have a grade
no, we have a student id

i dont undestand how to do it...but i understand what you mean by 3 characters.

>>i dont undestand how to do it

Ok, here's an example. Its not a complete program so you can't compile it as is.

string word;
ifstream in("infile.txt");
while( in >> word)
{
    if( word.length() == 3)
    {
           // this is a grade
    }
    else
    {
          // this is an ID
     }
}

if i want to use a data structure?

You can use a data structure just fine. If EVERY student has three exam codes, the struct I suggested earlier works fine. You can read them all in as integers and store them as such. If not every student has three exam codes, then Ancient Dragon's suggestion makes sense to me. You can determine when a new student starts when what you read in is not three characters. For that method you would need to read input as a string rather than an integer since reading data from a file as an integer will simply ignore leading zeroes, but reading it in as a string will not.

You need to know something about your data to decide what type of data structure you need. If not all students have three exam codes, the struct that I suggested earlier will not work. You will instead need a struct containing a student id and a vector or array of exam codes for each student id. Whether you store this data as strings or integers is up to you. Either can work as long as you are consistent. You can also read them in as strings but store them as integers. In order to proceed, you need to know what the data file looks like and what struct you want to use. What struct you decide to use will be influenced by what the data file looks like and what assumptions you can make about the data file.

my data is like this...
s1 0003
s1 0013
s1 0034
s1 0054
s1 0071
s1 0081
s1 0097
s1 0105
s1 0116
s1 0135
s1 0138
s2 0003
s2 0015
s2 0036
s2 0056
s2 0071
s2 0083
s2 0097
s2 0106
s2 0118
s2 0135
s2 0138
s3 0003
s3 0010
s3 0031
s3 0051
s3 0071
s3 0078
s3 0097
s3 0105
s3 0113
s3 0135
s3 0138
s4 0003
s4 0011
s4 0032
s4 0052
s4 0071
s4 0079
s4 0097
s4 0106
s4 0114
s4 0135
s4 0138
s5 0003
s5 0011
s5 0032
s5 0052
s5 0071
s5 0079
s5 0097
s5 0106
s5 0114
s5 0135
s5 0138
s6 0003
s6 0016
s6 0037
s6 0057
s6 0071
s6 0084
s6 0097
s6 0105
s6 0119
s6 0135
s6 0138
s7 0003
s7 0009
s7 0030
s7 0050
s7 0071
s7 0077
s7 0097
s7 0105
s7 0112
s7 0135
s7 0138

OK, it looks like each student has 11 exam codes and each student id is a string (i.e. "s7"). So how about a struct like this:

struct student
{
     string studentid;
     int examcode[11];
};

You can read in the data like this:

vector <student> students;
student aStudent;
while (stream1 >> aStudent.studentid)
{
     for (int i = 0; i < 11; i++)
          stream1 >> aStudent.examcode[i];

     students.push_back (aStudent);
}
#include<iostream>
#include<fstream>
using namespace std;
 
int main()
{
            struct student
		{
            int studentid;
            int examcode1;
            int examcode2;
            int examcode3;
            int examcode4;
            int examcode5;
            int examcode6;
            int examcode7;
            int examcode8;
            int examcode9;
            int examcode10;
            int examcode11;
		};

            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
		{
            cout << "While opening a file an error is encountered" << endl;
		}
            else
		{
            cout << "File is successfully opened" << endl;
		}	
            student aStudent;   
	     	while(!stream1.eof())
		{ 
            stream1 >> aStudent.studentid;
            stream1 >> aStudent.examcode1;
			stream1 >> aStudent.examcode2;
			stream1 >> aStudent.examcode3;
			stream1 >> aStudent.examcode4;
			stream1 >> aStudent.examcode5;
			stream1 >> aStudent.examcode6;
			stream1 >> aStudent.examcode7;
			stream1 >> aStudent.examcode8;
			stream1 >> aStudent.examcode9;
			stream1 >> aStudent.examcode10;
			stream1 >> aStudent.examcode11;

			cout << aStudent.studentid << endl;
			cout << aStudent.examcode1 << endl; 
			cout << aStudent.examcode2 << endl;
			cout << aStudent.examcode3 << endl;
			cout << aStudent.examcode4 << endl; 
			cout << aStudent.examcode5 << endl;
			cout << aStudent.examcode6 << endl;
			cout << aStudent.examcode7 << endl; 
			cout << aStudent.examcode8 << endl;
			cout << aStudent.examcode9 << endl;
			cout << aStudent.examcode10 << endl;
			cout << aStudent.examcode11 << endl;
		}
         
            return(0);
 
}

cant do it.............dont know how to make it

Replace this:

stream1 >> aStudent.studentid;
            stream1 >> aStudent.examcode1;
	    stream1 >> aStudent.examcode2;
	    stream1 >> aStudent.examcode3;
	    stream1 >> aStudent.examcode4;
	    stream1 >> aStudent.examcode5;
	    stream1 >> aStudent.examcode6;
	    stream1 >> aStudent.examcode7;
	    stream1 >> aStudent.examcode8;
	    stream1 >> aStudent.examcode9;
            stream1 >> aStudent.examcode10;
	    stream1 >> aStudent.examcode11;

with this:

stream1 >> aStudent.studentid;
            stream1 >> aStudent.examcode1;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode2;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode3;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode4;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode5;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode6;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode7;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode8;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode9;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode10;
            stream1 >> aStudent.studentid;
	    stream1 >> aStudent.examcode11;

Student id is in the file 11 times so you have to read it in 11 times instead of once. Hadn't noticed that before. Sorry.

#include<iostream>
#include<fstream>
using namespace std;

 
int main()
{
            struct student
		{
            string studentid;
            int examcode[11];
           
		};

            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
		{
            cout << "While opening a file an error is encountered" << endl;
		}
            else
		{
            cout << "File is successfully opened" << endl;
		}	
            vector <student> student;
			student aStudent;   
	      
            while (stream1 >> aStudent.studentid)
		{
            for (int i = 0; i < 11; i++)
            stream1 >> aStudent.examcode[i];

            students.push_back (aStudent);
		}  
		
            return(0);
 
}

Compiling...
11.cpp
C:\Documents and Settings\Administrator\Desktop\c++ master\15\11.cpp(25) : error C2065: 'vector' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\c++ master\15\11.cpp(25) : error C2275: 'student' : illegal use of this type as an expression
C:\Documents and Settings\Administrator\Desktop\c++ master\15\11.cpp(9) : see declaration of 'student'
C:\Documents and Settings\Administrator\Desktop\c++ master\15\11.cpp(28) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>
>' (or there is no acceptable conversion)
C:\Documents and Settings\Administrator\Desktop\c++ master\15\11.cpp(28) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

11.exe - 4 error(s), 0 warning(s)

commented: c++master? uhuh. -1

OK, if you're using the struct I suggested earlier as well as the layout, you need to add the following two lines at the top:

#include <string>
#include <vector>

You also have a typo here:

vector <student> student;

You need to name it something different from "student" since that is the name of the struct. Change it to this:

vector <student> students;

Also, keep in mind the correction I made in my last post that is not reflected here. You need to read in student id 11 times, not once, so change this:

while (stream1 >> aStudent.studentid)
	    {
                 for (int i = 0; i < 11; i++)
                      stream1 >> aStudent.examcode[i];

                  students.push_back (aStudent);
	    }

to this:

while (stream1 >> aStudent.studentid)
	    {
                 for (int i = 0; i < 10; i++)
                 {
                      stream1 >> aStudent.examcode[i];
                      stream1 >> aStudent.studentid;
                 }

                 stream1 >> aStudent.examcode[10];

                 students.push_back (aStudent);
	    }

This puts all your data in a vector of type "student" called students. If you want to display them all, you can do this after you are done reading them in (after the while loop):

for (int i = 0; i < students.size (); i++)
{
     cout << students.at (i).studentid << endl;
     for (int j = 0; j < 11; j++)
          cout << students.at (i).examcode[j] << endl;
}
#include<iostream>
#include<fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
            struct student
		{
            string studentid;
            int examcode[11];
			int aStudent;
           
		};

            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
		{
            cout << "While opening a file an error is encountered" << endl;
		}
            else
		{
            cout << "File is successfully opened" << endl;
		}	

            vector <student> students;
            while (stream1 >> aStudent.studentid)
	    {
            for (int i = 1; i < 12; i++)
		{
            stream1 >> aStudent.examcode[i];
            stream1 >> aStudent.studentid;
        }
            stream1 >> aStudent.examcode[11];
            students.push_back (aStudent);
	    }            
            for (int i = 1; i < students.size (); i++)
		{
            cout << students.at (i).studentid << endl;
            for (int j = 1; j < 11; j++)
            cout << students.at (i).examcode[j] << endl;
		} 

            return(0);
 
}

Compiling...
1.CPP
C:\Documents and Settings\Administrator\Desktop\c++ master\16\1.CPP(28) : error C2926: 'struct main::student' : types with no linkage cannot be used as template arguments
C:\Documents and Settings\Administrator\Desktop\c++ master\16\1.CPP(29) : error C2065: 'aStudent' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\c++ master\16\1.CPP(29) : error C2228: left of '.studentid' must have class/struct/union type
C:\Documents and Settings\Administrator\Desktop\c++ master\16\1.CPP(29) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

1.exe - 4 error(s), 0 warning(s)

>>while (stream1 >> aStudent.studentid)
when I compiled your code VC++ 2008 Express said aStudent is undefined.

yesss. so what must i do?

You accidentally took out a line that you had before. You want this before the while loop:

vector <student> students;
student aStudent;

Right now you have:

vector <student> students;

I do like this, Is it correct MAster
Vernon?

#include<iostream>
#include<fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
            struct student
        {
            string studentid;
            int examcode[11];
            int aStudent;

        };

            ifstream stream1("Standrews83.txt");
            char a[139];
            if(!stream1)
        {
            cout << "While opening a file an error is encountered" << endl;
        }
            else
        {
            cout << "File is successfully opened" << endl;
        }   

            vector <student> students;
            student aStudent;
            while (stream1 >> aStudent.studentid)
        {
            for (int i = 1; i < 12; i++)
        {
            stream1 >> aStudent.examcode[i];
            stream1 >> aStudent.studentid;
        }
            stream1 >> aStudent.examcode[11];
            students.push_back (aStudent);
        }            
            for (int i = 1; i < students.size (); i++)
        {
            cout << students.at (i).studentid << endl;
            for (int j = 1; j < 11; j++)
            cout << students.at (i).examcode[j] << endl;
        } 

            return(0);

}>>while (stream1 >> aStudent.studentid)

when I compiled your code VC++ 2008 Express said aStudent is undefined

Also, you have changed the indexes so that they start at 1 rather than 0. Indexes for vectors and arrays start at 0 in C++, not 1. So this:

while (stream1 >> aStudent.studentid)
	    {
            for (int i = 1; i < 12; i++)
		{
            stream1 >> aStudent.examcode[i];
            stream1 >> aStudent.studentid;
        }
            stream1 >> aStudent.examcode[11];
            students.push_back (aStudent);
	    }            
            for (int i = 1; i < students.size (); i++)
		{
            cout << students.at (i).studentid << endl;
            for (int j = 1; j < 11; j++)
            cout << students.at (i).examcode[j] << endl;
		}

should be this:

while (stream1 >> aStudent.studentid)
	   {
                for (int i = 0; i < 10; i++)
		{
                     stream1 >> aStudent.examcode[i];
                     stream1 >> aStudent.studentid;
                }
                stream1 >> aStudent.examcode[10];
                students.push_back (aStudent);
	   }
            
          for (int i = 0; i < students.size (); i++)
	  {
                cout << students.at (i).studentid << endl;
                for (int j = 0; j < 11; j++)
                    cout << students.at (i).examcode[j] << endl;
	  }

You are going to get a segmentation fault if you try to do this:

stream1 >> aStudent.examcode[11];

This is intentionally a loop of 10 rather than 11 in order to make the loop work. I've been told to stop using eof as my loop control so I made the while loop slightly different for that reason. There are other ways to do it, obviously, but this was my way. Feel free to change:

while (stream1 >> aStudent.studentid)
	   {
                 for (int i = 0; i < 10; i++)     // less-than-10, not less-than-11
		 {
                     stream1 >> aStudent.examcode[i];
                     stream1 >> aStudent.studentid;
                }
                stream1 >> aStudent.examcode[10];
                students.push_back (aStudent);
	   }

You don't need "int aStudent" in the struct. The struct is this:

struct student
{
     string studentid;
     int examcode[11];
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.