| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Struct/array help
Hello, i'm very new at C++... here's my program. I can't get it to compile..
please help [c++] #include <iostream> using std::cout; using std::cin; using std::endl; struct StudentRecord{ char Name[20]; int ID; float GPA; }; //================================================== =======main int main() { StudentRecord TESCStudent; TESCStudent.Name[]="SuperProgrammer"; TESCStudent.ID=1234; TESCStudent.GPA=4.0; return 0; } [\c++] |
|
#2
|
|||
|
|||
|
Several Reasons:
For example, this horrid piece of code works: |
|
#3
|
|||
|
|||
|
instead of
Code:
TESCStudent.Name[]="SuperProgrammer"; use Code:
memset(TESCStudent.Name, 0, 20); //clear out the string. it now appears to the computer that the Name string ends at the first character (i.e. an empty string) strncpy(TESCStudent.Name, "SuperProgrammer", 19); //copy SuperProgrammer into the string. the 19 makes sure that only 19 characters are copied (not 20 because we need at least one null char at the end so that the computer knows where the string ends) (this is unnecessary here, but entirely necessary when you don't know what string you are copying in, because if you write past the end of Name then (in this case) ID and GPA will be corrupted, and etc until your program crashes) |
|
#4
|
||||
|
||||
|
Well first off I would like to suggest you use the using namespace std; after your include <iostream> This allows you not to have to make a global call to std:: and you can simply use cout<< and cin>> you will see in my example below how I use them. Well I think your problem is more or less that you give the variable a size then you try and change the size later which is a big no no. A very easy way to solve this problem.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
struct StudentRecord
{
char Name[20];
int ID;
float GPA;
};
StudentRecord TESC_Student = {"SuperProgrammer",1234,4.0};
/*Creates an object of StudentRecord named TESC_Student with
the values in the {}. This is a very easy way to put values to
your structs especialy if there not going to be user defined.
just go in order seperated by comma's =)*/
int main (void)
{
/*Notice I just use cout<< not std::cout*/
/*Remember cout<< is for output to screen*/
/*cin>> is for input to program*/
cout<<TESC_Student.Name<<endl;
cout<<TESC_Student.ID<<endl;
cout<<TESC_Student.GPA<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Hope this helps you out a little, you'll still loose a small amount of memory due to your array but your just learning and you can pick that up as you go.
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. Last edited by Geo.Garnett : January 18th, 2006 at 04:30 PM. |
|
#5
|
|||
|
|||
|
I do appreciate it guys, i will try it asap and i'll get back ya. You guys are good.
Thanks worked perfectly. Again, I appreciate your help. |
|
#6
|
|||
|
|||
|
Hi guys i'm new to this forum and to c++...I have a similar problem..Here is my code
#include <iostream.h> #include <string.h> #include <stdlib.h> #include <iomanip.h> using std::cout; using std::cin; using std::endl; struct Movie{ char title[20]; int year; }; void getDetails(const Movie &); int main(){ Movie collection[5]; for(int i=0; i<5; i++){ getDetails(collection[i]); } return 0; } void getDetails(const Movie &m){ cout << "Please enter title: "; cin >> m.title; cout << "Enter year: "; cin >> m.year; } It wouldn't compile!! And what does Segmentation fault mean? |
|
#7
|
||||
|
||||
|
Remove the const from getDetails
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Struct/array help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|