Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 24-03-2008, 09:30 PM   #1 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Question what's the problem ?


see this code.........this works perfectly in turbo c, borland c 4.5 but gives a error in visual c++ 2008.

code:-
Quote:
#include<iostream.h>
#include<string.h>
class rab
{
public:
char r[25];
int h,b,l;
void disp();
void user();
};
void rab::user()
{
cout<<"Enter your name : ";
cin>>r;
}
void rab::disp()
{
l=strlen(r);
for(b=1;b<=l;b++)
{
cout.write(r,b);
cout<<"\n";
}
for(h=l;h>=0;h--)
{
cout.write(r,h);
cout<<"\n";
}
}
void main()
{
rab u;
u.user();
u.disp();
}
few more questions :-

what does "using namespace std;" means.................

this above code is out dated as per visual 2008 ..............so what is outdated in it..................
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer
clmlbx is online now  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-03-2008, 05:35 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: what's the problem ?

Quote:
Originally Posted by clmlbx View Post
what does "using namespace std;" means.................

this above code is out dated as per visual 2008 ..............so what is outdated in it..................
VS 2008 probably follows standard C/C++ I assume.

I/O operators such as cout and cin, belong to the std (Standard I/O) class so they obviously have to be referred to as:

Code:
std::cout and std::cin
Instead of just cout and cin like they used to be before standards were kicked into action. So using a 'std' namespace makes all functions and objects of it available to use directly without needing to associate a class ownership prefix.

For example, these both code samples below would work perfectly but you can see which one is sort of easier:

Code:
#include <iostream>

int main(void)
{
    std::cout<<"Using a :: operator to denote ownership"<<endl;
    return 0;
}
Code:
#include <iostream>

using namespace std;

int main(void)
{
    cout<<"Using a global namespace notation for std"<<endl;
    return 0;
}
Now if you are wondering what standards mean, its just like any other standards which apply to any product ensuring its quality and content remains the same worldwide.

Applying standards to code gives one great benefit: Your program becomes portable. A.k.a it runs on all kinds of platforms (Windows or *nix or Mac or etc) without the need of recompilation.

Now for a working version to your code:
Code:
#include <iostream>
// We exclude .h in standard code
// since the compiler automatically looks
// for the iostream class folders
// and includes the ones necessary.
// Same for all other standard headers.
#include <string>

using namespace std;

class rab
{
    public:
    char r[25];
    int h,b,l;
    void disp();
    void user();
};

void rab::user()
{
    cout<<"Enter your name : ";
    cin>>r;
}

void rab::disp()
{
    l=strlen(r);
    for(b=1;b<=l;b++)
    {
        cout.write(r,b);
        cout<<"\n";
    }
    for(h=l;h>=0;h--)
    {
        cout.write(r,h);
        cout<<"\n";
    }
}

int main(void)
// main() should always return an integer
{
    rab u;
    u.user();
    u.disp();
    return 0;
}
P.s. Do indent your code while writing it and also use the [CODE][/CODE] tags available. Improves readability.

Turbo C is of the yesteryears, its like a decade old. Things have drastically improved even in languages such as C/C++ since then. Standard code gives you more goodies like inbuilt Unicode support, better debugging and many more.

But yes, all code can't be standard. But what part might be, you have to code it that way for the sake of humanity.

Am attaching my compiled program of your code so that you can see what portable means. I compiled it over GNU C++ Compiler (g++) in Linux. It should run fine on your system too. ( Just rename the x.out to x.exe so that Windows can execute it. Heh, it needs an extension to execute, very stupid indeed. )
Attached Files
File Type: zip out.zip (3.8 KB, 2 views)
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 25-03-2008, 07:17 AM   #3 (permalink)
Fresh Stock Since 2005
 
Join Date: Feb 2005
Posts: 1,015
Default Re: what's the problem ?

Quote:
Originally Posted by clmlbx View Post
see this code.........this works perfectly in turbo c, borland c 4.5 but gives a error in visual c++ 2008.

code:-


few more questions :-

what does "using namespace std;" means.................

this above code is out dated as per visual 2008 ..............so what is outdated in it..................
where do you get the error??

i one I see is main being void. In new standards, main should always return a value and it should never be void. So make it int and return 0 which indicates successful run.. don't have VC 2008, and haven't analyzed the code.. cud u plz post the errors...

namespace have been introduced in new C++.. the older compilers do not understand namespaces......
__________________
http://www.khattam.info
khattam_ is offline  
Old 25-03-2008, 07:57 AM   #4 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default Re: what's the problem ?

^^ Error was that cout was unidentified ...i gues it has to be used like "std::cout and std::cin "as qwertymaniac said
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer
clmlbx is online now  
Old 25-03-2008, 09:41 PM   #5 (permalink)
Fresh Stock Since 2005
 
Join Date: Feb 2005
Posts: 1,015
Talking Re: what's the problem ?

Quote:
Originally Posted by clmlbx View Post
^^ Error was that cout was unidentified ...i gues it has to be used like "std::cout and std::cin "as qwertymaniac said

yes.. thats it.. alternately, you may write
using namespace std;
before main... sorry if it has already mentioned above by somebody.. too lazy to read...
__________________
http://www.khattam.info
khattam_ is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem With Data Recovary Software Problem Stellar Phoenix njm Software Q&A 1 08-05-2007 12:31 PM
Problem Related To Dvd and Iso Buring , any genius here who can resolve this problem vijayendra QnA (read only) 2 14-01-2007 09:38 AM
Problem!Problem!Problem Help Me November Cd And December dvd Doesnt Work yyy??? sourav_digit QnA (read only) 3 27-12-2006 02:33 PM
Serius problem with my screen. Can u tell it s/w or hard ware problem . tusarks Hardware Q&A 7 12-06-2006 01:58 PM
fedora cd iso problem(installation from hard drive problem) harmax Open Source 3 01-07-2005 11:34 AM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2