Jump to content

If,Switches, and Spaceport docking procedures

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Drago795

Drago795

    Newbie

  • Members
  • Pip
  • 5 posts
Hello there. I am new to the forums and this is only my second post, so please feel free to make any constructive criticisms or suggest any way I could make my posts or coding clearer and/or more efficient.
I am in the progress of making a fun little menu driven console program for a spaceship about to dock in a spaceport. Each time I learn a new concept in C++, ( which is pretty much everyday as I have only been learning for about 4 weeks) I intend to incorporate it into the program. However, i've already run into a snag in regards to If statements.
I'll go ahead post the code first and then below post what I am trying to do and what I have tried already.


#include <iostream>

#include <stdlib.h>


using namespace std;


int main()

{

    char cVesselClass;

    char cClassYesNo;

    bool bVesselError;

    do

    {

     std::cout << "Please enter your vessel class." << std::endl;

     std::cin >> cVesselClass;

     std::cout << "You are registered as Class " << cVesselClass << " is this correct? (y/n)" << std::endl;

     std::cin >> cClassYesNo;

                     if (cClassYesNo = 'Y')

                             {std::cout << "You may dock in one of the following docks: X,Y, or Z" << std::endl;}

                     else if (cClassYesNo = 'N')

                             {std::cout << "Please Re-enter your spaceship class." << std::endl;

                              bVesselError = true;}

     } while (bVesselError = true);

system("pause");

return 0;

}




What I am trying to do:
I want it to prompt the user for the class of his spaceship, display his choice back to him, query him if it is correct, and, if it is correct: show him the docks he can dock in, and, if it is incorrect: allow him to re-enter his class. This process should loop until the user is satisfied with the choice.

What the program is doing:
The program is displaying both possible outcomes, and looping, regardless of whether the user says the choice is correct or incorrect.

What I have tried:
-I have tried replacing the "else if" with another "if"
-replacing the "else if" with "else"
-replacing "=" with "==" in the brackets
-changing variable values from lowercase to upper case
-re-writing the program to instead use a "switch" command (which works but not how I wanted the program to work, plus i feel like it'll end up being less efficient and user friendly.)
---
This program was written in Dev-C++ but runs the same in MS VC+++ 2008 Express

Any help on this issue would be greatly appreciated. Thank you in advance.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
= is assignment, == tests for equality. You need to use == in your if statements.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Drago795

Drago795

    Newbie

  • Members
  • Pip
  • 5 posts
Hey, actually I tried that and now it is giving the correct response for the If statements but it is still looping back to prompting the user for a new vessel class.

Edited by Drago795, 16 October 2009 - 08:02 PM.
Incorrect response


#4
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
This is because you need to include a false condition under your yes If statement to break the loop.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You never set bVesselError to false. Also, make sure you aren't using = in your loop condition, or you'll set it back to true while testing.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Drago795

Drago795

    Newbie

  • Members
  • Pip
  • 5 posts
okay thanks, but just so i'm clear on the issue, do bool variables within if statements such as this always have to be false? Was the problem that I didn't initially initialize it as true and then set the condition to false? I feel like i'm misunderstanding something fundamental here.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The key is that in a do while loop, you will perform the loop UNTIL the while condition is false. If you never do anything to make it false (which depends on the condition), you'll never exit the loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Drago795

Drago795

    Newbie

  • Members
  • Pip
  • 5 posts
Oh okay, I got it now. Thank you.