Problem with classes and objects in simple program

0 John A · March 15, 2015
Hello everyone. I am currently watching Bucky's 12th C++ video and having received some unknown errors.

This is my code: 

#include <iostream>
#include <string>
using namespace std;

class arithmeticfunctions{
public:
  void quintuplet{

            cout << "sum" << endl;

  }; // [<----- First error here says "variable or field quintuplet declared void"]


    };


void additionfunction(){

    int x;
    int y;
    int z;
    int sum;

    cout << "Enter a number hassler" << endl;
    cin >> x;

    cout << "Enter another" << endl;
    cin >> y;

    cout << "z" << endl;
    cin >> z;




    sum = (x+y)/z;

    cout << sum << endl;


}

void subtractionfunction(){

    int x;
    int y;
    int z;
    int sum;

    cout << "Enter subtraction number" << endl;
    cin >> x;

    cout << "and a second" << endl;
    cin >> y;

    cout << "and a third" << endl;
    cin >> z;

    sum = x-y-z;

    cout << sum << endl;

}

int main()
{
    int x;

    cout << "Hello, welcome to the calculator!" << endl;
    cout << "For addition function, press 1. For subtraction function press 2. and for quint press 3" << endl;
    cin >> x;

    if(x == 1) additionfunction();
    if(x == 2) subtractionfunction();
    if(x == 3) arithmeticfunctions arithmeticobject;
                arithmeticobject.quintuplet();                // [<----Second error here says "arithmetic object was not defined in this scope"]
    return 0;
}



I have received two errors in the code which I have embedded notes next to describing the error. For the first error, I am not quite sure as to why it matters whether or not my function is void. It seemed to work when Bucky did it. The second error is very puzzling to me as I know my syntax is perfect. I must be missing some sort of fundamental point. Thank you in advance for the help.

Post a Reply

Replies

Oldest  Newest  Rating
+1 OLayemii Garuba · March 15, 2015
I've edited it for you.. and what's quintuplet suppose to do??

here's it

#include <iostream>
#include <string>
using namespace std;
class arithmethicfunctions{
public:
void quintuplet()
{
cout << "sum"<< endl;
}
void additionfunction(){
int x;
int y;
int z;
int sum;
cout << "Enter a number hassler" <<endl;
cin >> x;
cout << "Enter another" <<endl;
cin >> y;
cout << "enter third" <<endl;
cin >> z;
sum = (x+y)/z;
cout << sum <<endl;
}
void subtractionfunction(){
int x;
int y;
int z;
int sum;
cout << "Enter subtraction number" <<endl;
cin >> x;
cout << "and a second" << endl;
cin >> y;
cout << "and a third" << endl;
cin >> z;
sum = x-y-z;
cout << sum <<endl;
}
};
int main()
{
arithmethicfunctions arithObj;
int x;
cout << "Hello,welcome to the calculator!" <<endl;
cout << "For addition function, press 1. For subtraction function press 2. and for quint press 3" << endl;
cin >> x;
if(x == 1)
arithObj.additionfunction();
if(x == 2)
arithObj.subtractionfunction();
if(x == 3)
arithObj.quintuplet();
return 0;
}
+1 Moo Moo · March 15, 2015
I'm in no way a programmer. I struggle with maths, I started reading about programming 3 days ago or in short I am stupid, so don't think my word is gospel, haha.
Instead of re-writing your program to be amazing like the guy above me did (Actually i have no idea if it is indeed amazing, i'm assuming. :P ) I will address your errors specifically because I think that has more value for both of us!

But yeah, I thought it might be good practice to look through code of other beginners to find out what's wrong and here's what I found! (More experienced people feel free to correct me or tell me to fuck off or tell me what bad programming habits I use :P )

anyways, here goes:


First error: your quintuplet function has no ()  for entering parameters. (i think they are required even if you don't enter any.
so void quintuplet(){
body
}

is what you'd want.


Second error: I didn't even know you could use if statements without bodies, learning something new every day!
But i got your program to work by including the arithmeticobject.quintuplet() in the body of the if statement. I don't know why this works, but it does. It looks like this:

if(x == 3) {
arithmeticfunctions arithmeticobject;
arithmeticobject.quintuplet(); 
}


I am eager to find out what you'll do with that class though! (i assume the cout "sum" is just a placeholder) ... because I have no idea what a quintuplet is! haha!
+2 OLayemii Garuba · March 16, 2015
don't really need to be good in maths to be a great programmer :-)
0 John A · March 16, 2015
Thank you both very much. It seems my errors were basic syntax errors and not fundamental errors. I should have been a little more careful. Thanks again! Runs fine now.
+1 Umar ( ゚ヮ゚) · March 19, 2015

/* hey Pal! next time please type loooooooong codes in here */






Have Mercy on my eyes haha!:dizzy:
  • 1

C++

107,089 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator