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
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.
Re: Simple C++ OOP Question
Quote:
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
Re: Simple C++ OOP Question
Quote:
catastrophic error:
It's not tentative about its error messages is it :D
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;
}
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.
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
Re: Simple C++ OOP Question
Quote:
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.
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
Re: Simple C++ OOP Question
I don't think so. It seems to live OK with other Visual Studio products I installed.
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
Re: Simple C++ OOP Question
Use this tutorial http://www.learncpp.com
It's very good at covering the details
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
Re: Simple C++ OOP Question
Quote:
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.