Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ Beginner help
02-09-2010, 04:53 AM (This post was last modified: 02-09-2010 05:26 AM by Wheaties.)
Post: #1
C++ Beginner help
This is a story problem I have for homework. I am also writing the code for this I would just like to see if anyone could write the code so I could possibly compare and ask questions.

Teacher's Pet is a software firm that specalizes in children's educational programs. The firm has decided to devolop a series of products that will help children discover the properties of fractions. As you plan the program series, you realize, you realize that a fraction contains at least two data properties: a numerator and a denominator. Therefore you begin to think of specified fractions as objects that belong to the class Fraction. For now though, you decide to work with structs. Develop a Fraction Struct so that it contains public data fields that hold the integers numerator and denominator.(In later chapters you will change your data structure to a class and learn to make these data fields private.) Write a main () function that declares Fraction and allows you to enter data values for the two Fraction fields. Echo the input. Save the file as Fraction.cpp


ok so this should be fairly simple, for you guys at least. If you could help me out that would be awesome.







Don't laugh, this is what i have so far.


Code:
#include<iostream>

using namespace std;

struct Fraction
{
    int numerator;
    int denominator;
}
int Main()
{
    Fraction;

    cout <<"Enter numerator "<<endl;
    cin >> Fraction.numerator;
    cout <<"Enter denominator "<<endl;
    cin >> Fraction.denominator

    cout << "Your fraction is " << Fraction.numerator << "/" << Fraction.denominator<< endl;



    return 0
}
Find all posts by this user
Quote this message in a reply
02-09-2010, 05:02 AM (This post was last modified: 02-09-2010 05:11 AM by drdebcol.)
Post: #2
RE: C++ Beginner help
You are right, this is not a big problem !
Struct is type of data, you are basically declaring your own type !
So here is the code for declaring, reading and outputting struct that represents fraction :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

struct fraction //Declaring name of the struct
{  
   int numerator; //Things that are in the struct
   int denominator;
};

int main()
{
    fraction mynewfraction;  //Thise is like you declare your own type
    cin>>mynewfraction.numerator>>mynewfraction.denominator; //This reads first numerator and than denominator in the same line
    cout<<mynewfraction.numerator<<" "<<mynewfraction.denominator<<endl; //This writes them separated in one line
    system("pause");
}

I almost forgot. I wrote something about fraction operations in Pascal. But you have mathematical explanations there than can be used in C++ too.
I suppose that you want to make a whole class of "fraction struct" and to use it in "# include" area. So you can do 4 basic operations with fractions, or even more !
So here is the link :
http://www.pro9ramming.com/fraction-oper...t-812.html

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
02-09-2010, 05:28 AM
Post: #3
RE: C++ Beginner help
thanks so much dude. Very quick response and I wasn't too far off.

Cheers Smile
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: