|
-
February 11th, 2009, 12:20 AM
#1
Simple C++ OOP Question
Hi All members, hope everyone well and happy.
I am having a small problem with C++. I am very new to OOP with C++. The basic idea of this program was to sum all the Odd / even numbers between 1-100 depending on the users choice.
the code goes like this.
Code:
#include<iostream.h>
class cnt
{private:
ch='o';
int x,y;
public:
input()
{ x=0; y=0;
cout<<"Enter Choice:" ;
cin>>ch ;
return ch;
}
sumeven()
{ for(x=0;x<100;x++)
if(x%2==0)
y=y+x ;
return y;
}
sumodd()
{ for(x=0;x<100;x++)
if(x%2>0)
y=y+x ;
return y ;
}
void display()
{ cout<<"Reasult"<<y ;
}
};
void main()
{ cnt ob;
ob.input() ;
if (ch=='o')
ob.sumodd();
else
ob.sumeven() ;
ob.display;
}
On compling i am receiving an error message " for cannot be expanded inline"
I am using turbo c++ for windows.
Please help.
Regards
Amar
Try, Try Hard, Try Harder one day you will Succeed.
-
February 11th, 2009, 12:32 AM
#2
Re: Simple C++ OOP Question
Just checked out the code, a few mistakes:
-It is just iostream not iostream.h
-Your ch variable is missing a type (should be char).
-You can't initialise a variable like that in a class (as in char ch = 'o') unless it is static, so make a constructor to init it.
-Your class member functions are missing return types (ie, char input())
-The main() function cannot access ob's private data, so put if (ob.input()=='o') instead of ob.input() ; if (ch=='o') since cnt::input() returns a char.
-ob.display is missing arguments (Needs to be ob.display())
-Lastly, each of the functions needs to have a semicolon, ie void display()
{ cout<<"Reasult"<<y ;
}; <-- Semicolon goes here
This last point is the one which your compiler is complain, afaik. Functions with bodies defined inside the class declaration automatically inline themselves into code. If that does not help, perhaps try one of two things. Add curly braces for the body of the for loops (don't think this should do anything) or try to define the member functions separately, as in:
class cnt
{
public:
int sumeven();
};
int cnt::sumeven()
{
//code
}
Hope that helps
Last edited by FacelessJ; February 11th, 2009 at 12:36 AM.
-
February 11th, 2009, 05:15 AM
#3
Re: Simple C++ OOP Question
For gods sake dump turbo-C++ where it belongs. Stick it on a 5.25" floppy and donate it to a local museum.
You will never learn standard c++ while using a compiler thats neither modern or standards compliant.
You can get Microsoft visual c++ express here or the Dev-C IDE with mingw compiler here.
Both are far far better compilers than the archaic rubbish you are already using and both are totally free.
And get a good book. The code you posted is hideously flawed which is a sure sign your reference books/web tuts are terrible.
I recommend Accelerated C++ by Koenig and Moo as the very best starter book.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 11th, 2009, 05:38 AM
#4
Re: Simple C++ OOP Question
 Originally Posted by amarjitamarNew
I am having a small problem with C++. I am very new to OOP with C++. The basic idea of this program was to sum all the Odd / even numbers between 1-100 depending on the users choice.
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
iostream.h, line 1: catastrophic error: #error directive:
<iostream.h> is not a Standard header, use <iostream> instead.
Note that when you change this header name, that identifiers such as
"cout" and "endl" will no longer work, as they are in namespace
"std", so use be "std::cout" and "std::endl" respectively. See
http://www.comeaucomputing.com/techtalk/#cnames for more details
#error <iostream.h> is not a Standard header, use <iostream> instead. Note that when you change this header name, that identifiers such as "cout" and "endl" will no longer work, as they are in namespace "std", so use be "std::cout" and "std::endl" respectively. See http://www.comeaucomputing.com/techtalk/#cnames for more details
^
1 catastrophic error detected in the compilation of "ComeauTest.c".
Compilation terminated.
In strict mode, with -tused, Compile failed
Get another compiler. The standard header is <iostream>, and main() returns int, not void. Wherever you're currently learning C++, stop. The code you have uses old, outdated, and wrong concepts with respect to standard C++ (as of 1998).
Find another source of learning, preferably a good, modern, C++ book.
Regards,
Paul McKenzie
-
February 11th, 2009, 05:52 AM
#5
Re: Simple C++ OOP Question
It's not tentative about its error messages is it
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 11th, 2009, 08:09 AM
#6
Re: Simple C++ OOP Question
To display what you intend your program should be like the one below. I have used DJGPP compiler, The program runs fine and produces the result as you desire.
Code:
//WAP to sum all the even / odd numbers between 1-100 depending on the users choice
#include<iostream>
class total
{
int x,y,a,b,c;
public:
void input()
{
cout<<"\nEnter your choice:\n 1 for odd and 2 for even:";cin>>a;
if (a==1)
{
b=0;
for (x=1;x<100;x+=2)
{
b+=x;
}
cout<<b;
}
else if (a==2)
{
c=0;
for (y=2;y<=100;y+=2)
{
c+=y;
}
cout<<c;
}
else
{
cout<<"\nError! wrong option entered, please enter the option again.";
}
}
};
void main()
{
total display;
display.input();
return 0;
}
-
February 11th, 2009, 08:39 AM
#7
Re: Simple C++ OOP Question
You dont have to make classes for everything. For something this simple a single simple function is fine.
Your variable names stink to high heaven. Code should be self documenting and descriptive variable names go a long way towards this accomplishment.
1 statement 1 line is the best way for readability. Sticking a cin on the end of a cout is horridly unreadable.
cin and cout in your example are non-existant entities. There is no global cin or cout object in standard c++.
main always returns an int. a good compiler should crap out on void main in a hosted implementation.
DJGPP is an old compiler. It supports DOS programming. The latest version is 11 years old. Get yourself a decent compiler to learn with. Theres a link to 2 windows complete ide/compiler/linker/debugger packages in my sig.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 12th, 2009, 03:49 AM
#8
Re: Simple C++ OOP Question
Thanks for ur precious replies.
All i can say that it was the way we were taught, the way u have shown is much better. I will definitely shift to understanding and learinging a better C++ OOP.
I have Microsoft Visual Studio 6, can i use the Visual C++ ide to write these programs and test the code.
From the links that are given please tell me which one should i download from "bloodshed softwares for installing" There are host of links to download
Sorry to ask after all this help, can i get the pdf version of the book " Accelerated C++ by Koenig and Moo "
Require all ur support.
Best Regards
Amarjit
Try, Try Hard, Try Harder one day you will Succeed.
-
February 12th, 2009, 04:22 AM
#9
Re: Simple C++ OOP Question
 Originally Posted by amarjitamarNew
I have Microsoft Visual Studio 6, can i use the Visual C++ ide to write these programs and test the code.
I you're able to download the Visual Studio 2008 Express then I'd recommend that you do. VS 6.0 was finalised before the last specification for C++ was, and is therefore not the best platform to learn C++ on.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 12th, 2009, 06:19 AM
#10
Re: Simple C++ OOP Question
Thank u for your suggestion.
One more question before i try to download the Express Edition.
I have visual basic 6 in my system, will this installation interfere with the present vb6
regards
Amarjit
Try, Try Hard, Try Harder one day you will Succeed.
-
February 12th, 2009, 06:42 AM
#11
Re: Simple C++ OOP Question
I don't think so. It seems to live OK with other Visual Studio products I installed.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 13th, 2009, 01:13 AM
#12
Re: Simple C++ OOP Question
Thanks to all of you.
I have downloaded the Dec c+ compiler. Downloading the VB Express Edition is on process.
Regards
Amarjit
Try, Try Hard, Try Harder one day you will Succeed.
-
February 13th, 2009, 02:33 AM
#13
Re: Simple C++ OOP Question
Use this tutorial http://www.learncpp.com
It's very good at covering the details
-
February 16th, 2009, 12:56 AM
#14
Re: Simple C++ OOP Question
Thank you.
I have started using the Dev C++ software. As a beginner i think it will be sufficient for my present requirement.
I am having a small proble like the command window in which i would see the reasult, is getting vanished. I have tried to put a getch(); at the end of the code so that the screen halts and i may view the reasult. But getch() does not have any effect.
Any help how to stop the command window from getting vanished.
regards
Amarjit
Try, Try Hard, Try Harder one day you will Succeed.
-
February 16th, 2009, 02:03 AM
#15
Re: Simple C++ OOP Question
 Originally Posted by amarjitamarNew
Thank you.
I have started using the Dev C++ software. As a beginner i think it will be sufficient for my present requirement.
I am having a small proble like the command window in which i would see the reasult, is getting vanished. I have tried to put a getch(); at the end of the code so that the screen halts and i may view the reasult. But getch() does not have any effect.
Any help how to stop the command window from getting vanished.
regards
Amarjit
You can run your program from the command line, or set your IDE so that it automatically pauses the console window.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|