Find the answer to your Linux question:
Results 1 to 3 of 3
Hey, I've recently started programming C++. I've been following this book I got from the library and wanted to make a dorky little program based of one of the examples ...
  1. #1
    Just Joined! KPolulak's Avatar
    Join Date
    Feb 2009
    Location
    New Jersey, USA
    Posts
    42

    Post Calling All C++ Programmers: Need Help Debugging


    Hey,

    I've recently started programming C++. I've been following this book I got from the library and wanted to make a dorky little program based of one of the examples from the book. Here's the code (don't laugh):


    #include <iostream>

    using namespace std;

    int main()
    {
    int levelstart;
    int levelend;
    char direction;
    const int TOPFLOOR=20;
    const int BOTTOMFLOOR=1;

    cout << "*** WELCOME TO THE AWESOME ELEVATOR! ***" << endl << endl;
    cout << "What level are you on?";
    cin >> levelstart;
    cout << endl;

    cout << "Going up or down?";
    cin >> direction;
    cout << endl;

    cout << "Which floor are you going to?" << endl;

    if (direction="up")
    {
    while (levelstart!=TOPFLOOR)
    {
    cout << levelstart << endl;
    levelstart++;
    }
    }

    else
    if (direction="down")
    {
    while (levelstart!=BOTTOMFLOOR)
    {
    cout << levelstart << endl;
    levelstart--;
    }
    }

    cin >> levelend;

    while (levelend>=levelstart)
    {
    cout << levelstart;
    levelstart++;
    sleep 2;
    }

    while (levelend<=levelstart)
    {
    cout << levelstart;
    levelstart--;
    sleep 2;
    }

    cout << endl;
    cout << "You are now at level " << levelend << endl;

    return 0;
    }


    (Indentation doesn't show for some reason, sorry.)

    I got quite a few errors when compiling it and I was able to identify most and correct them except for these four:


    elevator.cpp: In function ‘int main()’:
    elevator.cpp:24: error: invalid conversion from ‘const char*’ to ‘char’
    elevator.cpp:34: error: invalid conversion from ‘const char*’ to ‘char’
    elevator.cpp:49: error: expected `;' before numeric constant
    elevator.cpp:56: error: expected `;' before numeric constant


    I absolutely cannot find these errors in my code. Anybody help?


    On a side note...how do I declare strings? I thought the proper way was to use the header file "#include <string>" and declare them "string stringname. I originally had "string direction" in place of "char direction" but that caused some problems when compiling.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by KPolulak View Post

    if (direction="up")
    if (direction="down")

    elevator.cpp: In function ‘int main()’:
    elevator.cpp:24: error: invalid conversion from ‘const char*’ to ‘char’
    elevator.cpp:34: error: invalid conversion from ‘const char*’ to ‘char’
    there is two of your errors direction is defined as a char and your setting it equal to a const char* ("up" and "down")...hope this helps Gerard4143

    Note you probably wanted a comparison == instead of = in your if statements

    Also I'm not a C++ programmer

  3. #3
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    You also need to declare how long direction is (C Strings - C Tutorial - Cprogramming.com). And use "sleep(2);" rather than "sleep 2;"

    If you want to format your code nicely, use the code tags.

    Put the word code in square brackets, then put in your code and close it like this with a /code tag like this: [/code]

    I can't show you the opening tag because it would be interpreted as my wanting to write code.

    Code:
    like this
      even indented
    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...