| | |
Pointer error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 108
Reputation:
Solved Threads: 7
I HAVE AN ASSIGNEMENT IN WHICH I GET STUDENT DATA AND SAVE IT A SEPARATE TEXT FILE , THE MAIN PROBLEM IS THAT IT TAKE ALL THE VALUES BUT DONOT STORE VALUE OF SEM,ADD-NO AND BATCH . KINDLY PLEASE HELP ME .
Coding is :-
Coding is :-
cpp Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> #include<fstream.h> struct stud { char *name; int roll; float gpa; char *sem; char *batch; char *add; float cgpa; }; int main() { clrscr(); fstream p("\\DATA.TXT",ios::out); int check; struct stud s; while(check!=4) { clrscr(); cout<<"\n\n\t\tWELLCOME TO DATABASE FOR STUDENTS(SEMESTER SYSTEM)"; cout<<"\n\n\t\t\t PRESS 1 FOR ENTERING DATABASE"; cout<<"\n\n\t\t\t PRESS 2 FOR VIEWING DATABASE"; cout<<"\n\n\t\t\t PRESS 3 FOR DELETING DATABASE"; cout<<"\n\n\t\t\t PRESS 4 TO EXIT"; cout<<"\n\n\n\n\t\t\tENTER YOUR OPTION : "; cin>>check; if(check==1) { clrscr(); cout<<"\n\nENTER STUDENT NAME : "; cout<<"\n\n********************************"; cout<<"\n\nENTER ROLL NUMBER : "; cout<<"\n\n********************************"; cout<<"\n\nENTER GPA : "; cout<<"\n\n********************************"; cout<<"\n\nENTER YOUR SEMESTER : "; cout<<"\n\n********************************"; cout<<"\n\nENTER YOUR BATCH : "; cout<<"\n\n********************************"; cout<<"\n\nENTER YOUR AD-NO : "; cout<<"\n\n********************************"; cout<<"\n\nENTER YOUR CGPA : "; cout<<"\n\n********************************"; gotoxy(21,3); cin>>s.name; gotoxy(21,7); cin>>s.roll; gotoxy(12,11); cin>>s.gpa; gotoxy(22,15); cin>>s.sem; gotoxy(19,19); cin>>s.batch; gotoxy(19,23); cin>>s.add; gotoxy(18,27); cin>>s.cgpa; //EOS for end of entry and EOL for end of line p<<s.name<<"(EOS)"<<s.roll<<"(EOS)"<<s.gpa<<"(EOS)"<<s.sem<<"(EOS)"<<s.batch<<"(EOS)"<<s.add<<"(EOS)"<<s.cgpa<<"(EOF)"; } } return 0; }
Last edited by WolfPack; Mar 26th, 2008 at 9:48 am. Reason: Added code tags. USe them when you post code.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
Please wrap code in code tags! This will preserve indentation you (hopefully) use when actually writing the code. The use of code tags is discussed in the watermarks of the message entry box and in the announcements at the top of the board.
Problems I see in the actual code:
1) It should be iostream and fstream without the .h extensions, and you should use a statement like: using namespace std;
2) conio.h is not a standard file and will not be come with all compilers so you may need to distribute it with any code you want to view on a different compiler.
3) struct stud s;
This is C style code. In C++ it's just:
stud s;
4) name, sem, add, batch are all char *. If you want to use them as strings then you have to declare memory for them to use. This is usually done with the keyword new. Here's an example.
char * name;
name = new char[10];
name can now hold up to nine useable char and a terminal null char. So you can now do something like:
cin >> name;
and actually get a value stored in name. But watch out, if the number of char actually entered is more than 9 you're in for a surprise!
5) if each separate data entry isn't separated by whitespace you're going to get a mess.
6) "EOS" and "EOL" seem reasonably odd ways to separate data in a file. Usually data is delimited by spaces or commas or dashes or semicolons and end of lines are either \r\n or \n depending on which operating system you're using.
You'll find you get better quality answers with better quality questions, so be as specific as possible when asking, include relevant input/output examples if appropriate, include error messages or warnings together with where in the code you are pointed to look for the errors/warnings.
Problems I see in the actual code:
1) It should be iostream and fstream without the .h extensions, and you should use a statement like: using namespace std;
2) conio.h is not a standard file and will not be come with all compilers so you may need to distribute it with any code you want to view on a different compiler.
3) struct stud s;
This is C style code. In C++ it's just:
stud s;
4) name, sem, add, batch are all char *. If you want to use them as strings then you have to declare memory for them to use. This is usually done with the keyword new. Here's an example.
char * name;
name = new char[10];
name can now hold up to nine useable char and a terminal null char. So you can now do something like:
cin >> name;
and actually get a value stored in name. But watch out, if the number of char actually entered is more than 9 you're in for a surprise!
5) if each separate data entry isn't separated by whitespace you're going to get a mess.
6) "EOS" and "EOL" seem reasonably odd ways to separate data in a file. Usually data is delimited by spaces or commas or dashes or semicolons and end of lines are either \r\n or \n depending on which operating system you're using.
You'll find you get better quality answers with better quality questions, so be as specific as possible when asking, include relevant input/output examples if appropriate, include error messages or warnings together with where in the code you are pointed to look for the errors/warnings.
Last edited by Lerner; Mar 25th, 2008 at 6:35 pm.
![]() |
Similar Threads
- free(): invalid pointer error - Help! (C++)
- pointer error (C)
- error with pointers (C++)
- RunTime error (Java)
- What's wrong with this strcpy and this pointer (C++)
- passing vector to a pointer? (C++)
- Error loading "C:\WINDOWS\ SYSTEM\BRIDGE.DLL' HELP (Windows 95 / 98 / Me)
- struct error (!?) (C)
Other Threads in the C++ Forum
- Previous Thread: Pythagoras Theorem in C++
- Next Thread: Open Same forminstance Multiple times
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






