Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2006, 04:01 AM   #1 (permalink)
joelw
Code Monkey
 
Join Date: Sep 2006
Posts: 36
joelw is on a distinguished road
help with creating and calling a function

I need help with creating and calling a function StudentData S2 = StudentData( S1 );

this what i got so far:
Code:
#include <iostream>

using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 

int main()
{
    int S1;
    int S2;
   
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
if anyone could help much appreciated thanks in advance.
joelw is offline   Reply With Quote
Old 12-05-2006, 07:35 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
As this is C++ I would use classes instead of structs.. And then just overload the standard operators.
Code:
#include <iostream>
#include <string>
class student
{
  private:
    student(void){}; // Impossible to create a student without a name
  public:
    std::string name;
    int major;
    float gpa;
    student(std::string n){name=n;};
    student(std::string n, int m){name=n; major=m;};
    student(std::string n, int m, float g){name=n; major=m; gpa=g;};
    student operator= (student s){ 
      name=s.name; 
      major=s.major; 
      gpa=s.gpa;
    };
};
std::ostream &operator<<(std::ostream &os, student s){
  os << s.name << std::endl;
  os << '\t' << s.major << std::endl;
  os << '\t' << s.gpa << std::endl;
  return os;
};

int main()
{
  student s1("felix", 4, 3.9);
  student s2 = s1;
  std::cout << s1;
  std::cout << s2;
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 12-05-2006 at 07:49 AM.
redhead is offline   Reply With Quote
Old 12-05-2006, 11:28 AM   #3 (permalink)
joelw
Code Monkey
 
Join Date: Sep 2006
Posts: 36
joelw is on a distinguished road
ok but could it be done in stuct basically i need to create and call a function called StudentData S2 = StudentData( S1 ); The function receive as a parameter a reference to the structure( prototyping will handle this) and will return a reference to the structure. Use couts and cins for getting data from the user. (For testing puposes, change the data in S1 so that the GPA is 3.5 and the Major is 2.)

if you can give me an example of this.
Thanks in advance.
joelw is offline   Reply With Quote
Old 12-05-2006, 06:58 PM   #4 (permalink)
albtross
Recruit
 
Join Date: Nov 2006
Posts: 9
albtross is on a distinguished road
like this??
Code:
#include<iostream>
using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 
Student studentdata(Student&);

int main()
{
	struct Student s1;
	struct Student s2;
	cout << "please enter your name: \n";
	cin >> s1.Name;
	cout << "please enter your GPA: \n";
	cin >> s1.GPA;
	cout << "please enter your major: \n";
	cin >> s1.Major;
	s2=studentdata(s1);
	cout << s2.GPA <<endl;
	cout << s2.Major << endl;
	cout << s2.Name << endl;
      
    return 0;

}
Student studentdata(Student &s1)
{
	cout << "please enter your name: \n";
	cin >> s1.Name;
	cout << "please enter your GPA: \n";
	cin >> s1.GPA;
	cout << "please enter your major: \n";
	cin >> s1.Major;
	return s1;
}
albtross is offline   Reply With Quote
Old 12-06-2006, 02:22 AM   #5 (permalink)
joelw
Code Monkey
 
Join Date: Sep 2006
Posts: 36
joelw is on a distinguished road
thanks and now if i need to call a function called ChangeData with a pointer to S2 as the argument ChangeData( &S2 ); changing the data in S2 so that the GPA is 3.0 and the Major is 1 would i do that the same way as the first function?
joelw is offline   Reply With Quote
Old 12-06-2006, 06:54 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Look at what studentdata() does in albtross example, and use the same method. We are not here to do your homework.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-13-2006, 02:56 AM   #7 (permalink)
joelw
Code Monkey
 
Join Date: Sep 2006
Posts: 36
joelw is on a distinguished road
ok so far this is what ive got for my code im trying to figure out how to start my for loop that will receive the array and the int that represents the count(2)?

Code:
#include<iostream>
using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 

//Function Prototype
Student studentdata(Student&);

//Function Prototype
Student changeData(Student&);

//Function Prototype
Student GetStudents(Student&);

int main()
{
	struct Student s1;
	struct Student s2;
	struct Student s3;
	
	s2 = studentdata(s1);
	s2 = changeData(s2);
	s2 = GetStudent(s3);
    cout << "GPA: " << s2.GPA <<endl;
	cout << "Major: " << s2.Major << endl;
	cout << "Students Name: " << s2.Name << endl;
      
     return 0;

}
Student studentdata(Student &s1)
{
	cout << "please enter your name: ";
	cin >> s1.Name;
	cout << "please enter your GPA: ";
	cin >> s1.GPA;
	cout << "please enter your major: ";
	cin >> s1.Major;
	return s1;
}

Student changeData(Student &s2)
{
   s2.GPA = 3;
   s2.Major = 1;
   return s2;   
}

Student GetStudents(Student &s3)
{
        for (
joelw is offline   Reply With Quote
Old 12-22-2006, 08:54 AM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
The rest of the code is missing. Be accurate. Even when you post
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting