what's wrong with this code

This is a discussion on what's wrong with this code within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int main() { for (char x) ( char x; cout<<" Please imput name."; cin>>x cout<<" Welcome,"<<char x; ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    14

    Unhappy what's wrong with this code

    Code:
    #include <iostream.h>
    int main()
    {
     for (char x)
      (
       char x;
       cout<<" Please imput name.";
       cin>>x
       cout<<" Welcome,"<<char x;
      )
     return 0
    }
    what's wrong with it?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The for loop, the brackets and a redeclaration.

    What does your compiler complain about ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Could you change it to make it work or something?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The use of pre-standard headers is bad too.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    oh and it says something about backward_warning.h

  6. #6
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    Perhaps you could explain why you wanted to use a for loop here. Also, a char variable will only hold one character, so it won't be able to hold a name like you want it to. You will have to look into strings for that. When you go to cout a variable, you do not need to specify its type beforehand, just say the variable name.

  7. #7
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    wrong bracket usage
    redeclaration
    old headers
    why a loop here???

    Here is a simple code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    cout << "Please input your name: ";
    char string[256]; // string with max. 256 characters
    cin.getline(string,'\n');
    
    cout << "\nWelcome " << string << endl;
    
    system("pause");    // to keep the window open
    
    return 0;
    }
    Last edited by codingmaster; 06-01-2003 at 11:30 AM.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You might wanna #include <cstdlib> just to be safe with that system( ) call.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    well, this is going to sound very nooby, the fact is that when an aplication is done and the finaltext is being viewed it immediately shuts down, giving me no time to read it. Thats why I made a loop to let it start agin so I can read the text

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    That's what system( "pause" ); does. It waits for a keypress.

    If you had just read the FAQ, you might have found something pertaining to your problem.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    I use "getch()" as a pause, "system("pause")" I heard isn't a good idea to do. (I heard system commands in general were a bad idea )

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Nuke
    I use "getch()" as a pause, "system("pause")" I heard isn't a good idea to do. (I heard system commands in general were a bad idea )
    system() is bad because it's slow and non-portable. What it does is run whatever command it takes as its paramater as though you typed it into the shell (try typing "pause" into the shell to see for yourself). Since "pause" is only good on Windows (as far as I know), it's not portable to say system("pause").

    But AFAIK it's equally unportable (albeit quicker) to use getch() because that mainly exists in Windows environments. What I usually do is declare my own non-portable functions in a single header file and simply replace that file when I switch platforms. You could define a void myPause() function in the header. The Windows version could call getch() and other versions can do other things.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How about a portable solution?
    Code:
    inline void wait_for_keypress()
    {
      std::cin.ignore(std::cin.rdbuf->in_avail() + 1);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    4
    I don't understand why you are using a for loop also. But isn't the problem : you forgot

    Code:
    cin >> x;
    i think that it ????????

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    -- begin snip of error code --
    #include <iostream.h>
    int main()
    {
    for (char x)
    /* for checks for a positive condition and it is getting 0
    the proper syntax in a for loop is
    for (int i = 0; i < 10; i++)
    {
    }
    if your looking for a forever loop
    for (1)
    {
    }
    /*
    (
    char x; // this is a variable that is only one character. char is not string.
    cout<<" Please imput name.";
    cin>>x
    cout<<" Welcome,"<<char x;
    )
    return 0
    }
    -- end snip --

    -- begin snip of corrected code --
    #include <iostream>
    #include <string>

    int main(int argc, char* argv[])
    {
    std::string MyString;
    for(int i = 0; i <= 10; i++)
    {
    std::cout << "Please enter your name: ";
    std::getline(std::cin, MyString);
    std::cout << "Welcome " << MyString << std::endl;
    }
    return 0;
    }
    -- end snip --
    Last edited by Phylter; 06-03-2003 at 11:15 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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