C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 18th, 2006, 12:57 AM
2cool 2cool is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 4 2cool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 14 m 41 sec
Reputation Power: 0
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++]

Reply With Quote
  #2  
Old January 18th, 2006, 02:45 PM
Neuro Neuro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 7 Neuro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 10 m 47 sec
Reputation Power: 0
Several Reasons:
  • The C++ standard forbids direct assignment of arrays like that
  • You do not need the '[]' because you have already assigned the size of the array and [] tells the program to work out the size needed.
  • Your name is less than 20 chars and petit though it seems, your compiler might not like that. I'm not joking - I just pasted that code into Dev-Cpp (win32)

For example, this horrid piece of code works:
C++ Code:
Original - C++ Code
  1. int main()
  2. {
  3.    
  4. StudentRecord TESCStudent;
  5. char* blah;
  6. blah = (char*) malloc(20);
  7. cin >> blah;
  8. strcpy(TESCStudent.Name,blah);
  9. cout << blah << "\n" << TESCStudent.Name << "\n";
  10. TESCStudent.ID=1234;
  11. TESCStudent.GPA=4.0;
  12. cin >> blah;
  13.  
  14. return 0;
  15. }

Reply With Quote
  #3  
Old January 18th, 2006, 03:18 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 14
Send a message via AIM to ubergeek
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)

Reply With Quote
  #4  
Old January 18th, 2006, 03:27 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 13
Send a message via AIM to Geo.Garnett
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 03:30 PM.

Reply With Quote
  #5  
Old January 18th, 2006, 03:40 PM
2cool 2cool is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 4 2cool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 14 m 41 sec
Reputation Power: 0
Talking

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.

Reply With Quote
  #6  
Old March 10th, 2006, 06:36 AM
kyt kyt is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Dublin, Ireland
Posts: 1 kyt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 57 sec
Reputation Power: 0
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?

Reply With Quote
  #7  
Old March 10th, 2006, 02:11 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Remove the const from getDetails

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Struct/array help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap