LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 11-24-2004, 08:06 PM   #1
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Problems practicing C++ classes


I'm independently trying to learn C++ - and wow is it a rought road not having known C...
But, nevertheless, I am persistent. I'm having a problem with the following program. Every time I try to compile, I get these errors:
Code:
calc.cpp: In function `int main()':
calc.cpp:65: error: `calculcation' undeclared (first use this function)
calc.cpp:65: error: (Each undeclared identifier is reported only once for each
   function it appears in.)
Now, I realize the way I'm doing everything is really too much, but at this point I'm just trying to learn how passing variables by value and classes are supposed to work. I think this is right, but it won't compile.
Code:
//calc.cpp
//an experiment to write a calculator using C++ classes

/* Program will not compile - generating these error messages:

calc.cpp: In function `int main()':
calc.cpp:65: error: `calculcation' undeclared (first use this function)
calc.cpp:65: error: (Each undeclared identifier is reported only once for each
   function it appears in.)

*/

#include <iostream>
using namespace std;

class calc { //calc class - performs calculations and returns results
	float ans;
public:
	float add(float add1, float add2);
	float sub(float sub1, float sub2);
	float mult(float mult1, float mult2);
	float div(float div1, float div2);
};

float calc::add(float add1, float add2) {
	ans = add1 + add2;
	return ans;
}

float calc::sub(float sub1, float sub2) {
	ans = sub1 - sub2;
	return ans;
}

float calc::mult(float mult1, float mult2) {
	ans = mult1 * mult2;
	return ans;
}

float calc::div(float div1, float div2) {
	ans = div1 / div2;
	return ans;
}


int main() {

	char done;
	int choice = 0; // main() local variable declaration to control program flow
	float num1 = 0, num2 = 0;
	calc calculation; // declare variable 'calculation' of type calc
			
	cout << "Welcome to my experimental calculator!\n";
	do {
		choice = 0;
		num1 = num2 = 0;
		cout << "Please select a calculation: \n";
		cout << " 1) Add\n 2) Subtract\n 3) Multiply\n 4) Divide\n";
		cout << "Choice: ";
		cin >> choice;
		cout << "\nPlease enter your first value: ";
		cin >> num1;
		cout << "\nPlease enter your second value: ";
		cin >> num2;
		cout << "\n";
		switch ( choice ) {
			case 1:
				cout << "Your answer is: " << calculation.add(num1, num2);
				// This is the code it's throwing errors on
				break;
			case 2:
				cout << "Your answer is: " << calculcation.sub(num1, num2);
				break;
			case 3:
				cout << "Your answer is: " << calculation.mult(num1, num2);
				break;
			case 4:
				while (num2 = 0) {
					cout << "Can't divide by zero. Please re-enter your value: ";
					cin >> num2;
					cout << "\n";
				}
				cout << "Your answer is: " << calculation.div(num1, num2);
				break;
			default:
				cout << "Please enter a selection.\n";
				break;
		}
		cout << "\nAre you finished? (y/n): ";
		cin >> done;
	} while (done = 'n');
	cout << "\n";
	return 0;
}
I'm going to feel really stupid if it's something like a missed semicolon again...
 
Old 11-24-2004, 08:52 PM   #2
SuperCoffeeMan
Member
 
Registered: Oct 2004
Location: Salem, Oregon
Distribution: Tri-Boot (Ubuntu, OS X, XP)
Posts: 47

Rep: Reputation: 15
Code:
g++ -o calc calc.cpp
calc.cpp:1: error: expected unqualified-id before '/' token
calc.cpp:1: error: expected `,' or `;' before '/' token
In file included from /usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/3.4.1/iostream:44,
                 from calc.cpp:13:
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/3.4.1/i586-mandrake-linux-gnu/bits/c++config.h:61: error: expected namespace-name before ';' token
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/3.4.1/i586-mandrake-linux-gnu/bits/c++config.h:61: error: `<type error>' is not a namespace
calc.cpp: In function `int main()':
calc.cpp:72: error: `calculcation' undeclared (first use this function)
calc.cpp:72: error: (Each undeclared identifier is reported only once for each function it appears in.)
I copied the code into vim, then tried to g++ it, and I got this error message. Although, you must know, I do not know C++. Hope it helps.
 
Old 11-24-2004, 09:27 PM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
There is another error in your code

You have used:
while (num2 = 0)

it should read:
while (num2 == 0)

Also
while (done = 'n')

should change to
while (done == 'n')

You need to compare using == and not =

Last edited by vharishankar; 11-24-2004 at 09:35 PM.
 
Old 11-24-2004, 09:33 PM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Your error is a simple typo:

In Line 72 you have

calculcation

Change it to
calculation

And it should work fine!!!!
 
Old 11-24-2004, 09:38 PM   #5
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Original Poster
Rep: Reputation: 47
Quote:
Originally posted by Harishankar
Your error is a simple typo:

In Line 72 you have

calculcation

Change it to
calculation

And it should work fine!!!!
Know what's really sick? I muct've checked my spelling 10 times and was positive it was correct in all variables. *shakes his head*
Thanks
 
Old 11-24-2004, 09:41 PM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Also make sure you change the comparison statements to == instead of =. I noted that change in my earlier post. That wont give you an error but with lead to a logic bomb because the statement will 'ASSIGN' the value to that variable instead of COMPARING.
 
Old 11-24-2004, 10:16 PM   #7
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Original Poster
Rep: Reputation: 47
Yes - I changed that thank you. Any idea of a method of error checking? I tried this, but if I enter an invalid option, say, pushing the "Up" arrow on my keyboard and generating ^[[A as input, it freaks out and crashes into an infinite while loop.
Code:
while ( (choice >= 0 ) && ( choice <= 5 ) ) {
Apart from really wierd input though, if I enter something such as 'a', the default: handles it.
 
Old 11-24-2004, 10:24 PM   #8
rekurzion
Newbie
 
Registered: Nov 2004
Distribution: Ubuntu 4.10 (2.6.8.1-3-386) and loving it.
Posts: 15

Rep: Reputation: 0
using a try...catch and handling the exception would give you control over invalid input.

But honestly, starting with C++ rather than C is the hard part. Starting with OO programming and not understanding the basic constructs of the language is rather difficult. It is a much larger jump. However, read up on exception handling and that should solve you problem.

If you are really interested in OO programming however, try Java and use Eclipse, it's a no brainer.
 
Old 11-24-2004, 10:49 PM   #9
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Quote:
Originally posted by scuzzman
Yes - I changed that thank you. Any idea of a method of error checking? I tried this, but if I enter an invalid option, say, pushing the "Up" arrow on my keyboard and generating ^[[A as input, it freaks out and crashes into an infinite while loop.
Code:
while ( (choice >= 0 ) && ( choice <= 5 ) ) {
Apart from really wierd input though, if I enter something such as 'a', the default: handles it.
Instead of using a cin statement which requires a ENTER key press after you type in the option, you could use a getch () statement which is defined in curses.h.

Do a lookup for getch () in the unix man pages. There is a lot of useful info on getting keyboard input.
 
Old 11-24-2004, 10:50 PM   #10
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Original Poster
Rep: Reputation: 47
Well, I have 1) a mother who works for McGraw-Hill publishing and 2) a 50,000 book virtual library I can access from home offered by my college - I think I have plenty of books
I was going to use Java - but I have yet to find a practical use for it. My goal is to become at least useful in the OpenSource world as, at very least, a beta-tester, which it would help if I could code. I figure: a lot of Linux apps (including all of Gnome) these days are written in cpp, so I'd start with that.

<EDIT>
Afterthought: is it way too geeky if the main reason I want to learn to code is so I can write open-source applications and not for personal gain? lol
</EDIT>

Last edited by scuzzman; 11-24-2004 at 10:52 PM.
 
Old 11-24-2004, 10:53 PM   #11
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
The man page for getch() also includes a lot of info on how to get special characters like arrow keys, function keys and so on from the terminal.
 
  


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
Classes in VC++ Diederick Programming 2 11-30-2005 11:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 11:10 PM
classes in C++ niteshadw Programming 2 07-05-2005 08:05 PM
C++ nested classes enemorales Programming 5 05-23-2005 04:40 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 12:45 PM


All times are GMT -5. The time now is 04:45 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration