LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2011, 08:55 PM   #1
mrplopez1972
LQ Newbie
 
Registered: Jan 2011
Posts: 1

Rep: Reputation: 0
Unhappy C++ question on Structs


Please tell me how to incorporate these two structs in the same progam:

struct Name
{
string first;
string middle;
string last;
};
Name yourName
Name myName



with....



struct studentRecord
{
Name studentName;
Name teacherName;
int gradeNumber;
string grades;
}
studentRecord sally;



I have worked at this for 4 hours now.
 
Old 01-23-2011, 09:21 PM   #2
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
name.cpp
Code:
#include <stdio.h>
using namespace std;

typedef struct
{
   string first;
   string middle;
   string last;
}Name;

typedef struct
{
   Name   studentName;
   Name   teacherName;
   int    gradeNumber;
   string grades;
}studentRecord;

int main () 
{
     studentRecord sally;
}
What were you asking here exactly? The above shown code is compiling properly!
Code:
anisha@linux-uitj:~> g++ name.cpp -Wall -Wextra
anisha@linux-uitj:~>
 
Old 01-23-2011, 10:59 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Anisha Kaul View Post
What were you asking here exactly? The above shown code is compiling properly!
Nothing personal, but that's C written in C++ (or C++ written in C?)

A good place to start is http://www.cplusplus.com/; look for C++ Language Tutorial.
Kevin Barry

Last edited by ta0kira; 01-23-2011 at 11:01 PM.
 
Old 01-23-2011, 11:07 PM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by ta0kira View Post
Nothing personal, but that's C written in C++ (or C++ written in C?)
I initially compiled the code as a C file, then realized that he did use the type string, so using namespace std; had to be included, so I just changed the extension of that file to .cpp and compiled it through g++!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C: structs and pointers question kpachopoulos Programming 3 03-27-2006 05:54 PM
copy structs in c alaios Programming 10 09-10-2005 02:31 PM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 05:41 PM
static structs? simbo Programming 3 02-05-2004 04:00 AM
Self referential structs in C? MadCactus Programming 14 01-28-2004 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration