To use words for a variable

This is a discussion on To use words for a variable within the C++ Programming forums, part of the General Programming Boards category; Code: //To use a persons name #include<iostream.h> main() { int a; char c, cout<<"Enter name"<<"\n"; cin>>c>>; cin.get(); cout<<"age"<<"\n"; cin>>a; if(a=xyz) ...

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    Smile To use words for a variable

    Code:
    //To use a persons name
    
    
    #include<iostream.h>
    main()
    {
    
    int a;
    char c,
    cout<<"Enter name"<<"\n";
    cin>>c>>;
    cin.get();
    cout<<"age"<<"\n";
    cin>>a;
    
    if(a=xyz)
     {
         cout<<"abc"<<"\n";
    cin.get();
    }
    return 0;
    }
    The problem is that i cannot use a=xyz. I am a good QBasic Programmer. In QBasic we use

    10 x=asd THEN PRINT "afddafadad";
    Is there something similar to this in C++?


    Saimadhav

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    std::string (which you would need to use #include <string> to make use of std::string) would be the right type for your variable. Obviously, xyz is not a string [in any sense]. You probably wanted to say
    Code:
    if (c == "xyz")
    .

    You should also avoid the old style <iostream.h> in preference of <iostream> - although you may find that the 10+ years old Turbo C is not suitable for modern C++ compiling, so you may need to update to a later compiler - there are free ones around, and suggestions for those if you search the forum - there's usually one or two suggestions of this per week.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Your code is invalid C++.

    Code:
    #include<iostream.h> // Deprecated header.
    main() // ERROR: Missing explicit return type (should be int).
    {
    int a;
    char c, // ERROR: Declaration does not end with ;
    cout<<"Enter name"<<"\n";
    cin>>c>>; // ERROR: Missing identifier after "c>>".
    cin.get();
    cout<<"age"<<"\n";
    cin>>a;
    
    // ERRORS:
    // 1. a is a char, not a string.
    // 2. = is assignment, not comparison.
    // 3. xyz is interpreted as a variable, not a string.
    if(a=xyz)
     {
         cout<<"abc"<<"\n";
    cin.get();
    }
    return 0;
    }
    You need to go back to your books!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Saimadhav View Post
    Code:
    //To use a persons name
     
     
    #include<iostream.h>
    main()
    {
     
    int a;
    char c;
    cout<<"Enter name"<<"\n";
    cin>>c;  // removed the apparently unnecessary >>
    cin.get();
    cout<<"age"<<"\n";
    cin>>a;
     
    if(a=='xyz')
     {
         cout<<"abc"<<"\n";
    cin.get();
    }
    return 0;
    }
    The problem is that i cannot use a=xyz. I am a good QBasic Programmer. In QBasic we use

    10 x=asd THEN PRINT "afddafadad";
    Is there something similar to this in C++?


    Saimadhav
    there. BTW thats not very good Qbasic either, but thats a topic for another board
    Last edited by abachler; 07-09-2008 at 09:20 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Comparing a char to 'xyz'?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> #include<iostream.h> // Deprecated header.

    Note that <iostream.h> is not deprecated by the standard because it is completely non-standard. Deprecated implies that it is allowed but discouraged.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Expanding on what Elysia said, and what you changed once already...
    Code:
    //To use a persons name
     
     
    #include<iostream.h>
    main() //ERROR: still needs a return type
    {
     
    int a;
    char c;
    cout<<"Enter name"<<"\n";
    cin>>c;  // removed the apparently unnecessary >>
    cin.get();
    cout<<"age"<<"\n";
    cin>>a;
     
    if(a=='xyz') //ERROR: wrong use of ' '
     {
         cout<<"abc"<<"\n";
         cin.get();
    }
    return 0;
    }
    Last edited by scwizzo; 07-09-2008 at 10:27 AM.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Gah, you are right, its nto even comaprign a char to 'xyz' its comparing int to 'xyz', evene worse. Sorry, the air is ou tin our office and it's a bazillion degrees here
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 07:47 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21