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 ...
- 02-24-2009 #1
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.
- 02-24-2009 #2
- 02-24-2009 #3Linux 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.
Let us know how you get on,Code:like this even indented
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.


Reply With Quote
