C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,574 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,194 people online right now. Registration is fast and FREE... Join Now!




Why is my program not breaking the loop?

 

Why is my program not breaking the loop?, it seems to loop even when i dont want it to

Pipey85

3 Jul, 2009 - 12:51 AM
Post #1

New D.I.C Head
*

Joined: 30 Jun, 2009
Posts: 27

Ok first, ive changed some the text as some of you lovelly people may find it offensive, this is my first attempt at something like this in terms of size, it just seems that the statements that are pointed out just keep looping even when i try to break the loop.

CODE

#include "stdafx.h"
#include <iostream>
using namespace std;

int c1smoke(int xp);
int c1save(int xp);


void GO ();

int main()
{    
    int RampagePoints =0;
    int xp = 0;
    int DChoice;
    bool start = true;
    char strt, BC;



    while (start == true)
    {        
    start = false;
        cout << "please choose an option\n\n";
        cout << "1.number1\n2.number2\n3.number3\" << endl;
        cin >> DChoice;
        cout << endl << endl;

    switch (DChoice)
    {


        case 1:
            {
        int choice1;
        bool cse1strt = true
        ;
        while (cse1strt = true)    // this loop here keeps going eveytime i make a choice from the point
        {                                 // marked "point !"
            cse1strt = false;
        cout << "You do something....... gaining 1 xp";
        ++xp;
        cout <<    "\n Now Do you\n1.do this\n2.do that?\n";
        cin >> choice1;

            switch (choice1)
            {
        case 1: //inside switch (choice1)
            {
                xp = c1smoke (xp);
                cse1strt = false;
                break;               \\ Point ! why is it doing this?
            }
        case 2:
            {
                c1save (xp);
                break;
            }
        default:{
            cout << "That's not an option, try again!";
            cse1strt = true;
            break;
                }
            } // end of switch (choice1)

        
        } // end of while cse1strt
            }//end of case1
        case 2:
    {
        cout << "not got this far\n\n\n";
        ++xp;
        break;
    }
        case 3:
    {
        cout << "coming soon\n\n\n";
        ++xp;
        break;
    }
                default:
    {
        cout << "Are you high? That wasnt an option!\n";
    }
    
    

    cout << endl << "total xp = " << xp;
    cout << endl << "Would you like to try a different drug?";
    cin >> strt;

    if (strt == 'y' || 'Y')
    {
        cout << endl << endl << endl;
        start = true;
    }
    else
        start = false;



    } // end of switch
    return 0;
    }// end of while start
}//end of main

void GO ()
{
    cout << "GAME OVER!!";
}
int c1smoke(int xp)
{
char any;
    cout << "YAY  >>> you gain 1 xp";
    ++xp;

    
    return xp;





}//end of c1smoke


int c1save(int xp)
{
    return xp;
}



I hope its not to difficult to understand beacause of the text edits, but i really dont want to offend anybody.

so any ideas as to why the loop wont break when i ask it to? thanks in adavnce guys (and gals) im sure this is no match for your mighty knowledge tongue.gif

User is offlineProfile CardPM
+Quote Post


deery5000

RE: Why Is My Program Not Breaking The Loop?

3 Jul, 2009 - 01:34 AM
Post #2

D.I.C Regular
***

Joined: 9 May, 2009
Posts: 354



Thanked: 20 times
My Contributions
CODE

case 1: //inside switch (choice1)
{
    xp = c1smoke (xp);
                 cse1strt = false;
                break;              
}



This break point takes you out of your switch not your while loop,

CODE

default:{
    cout << "That's not an option, try again!";
    cse1strt = true;
    break;
    }

} // end of switch (choice1)

break;


Add this extra break point to allow you to exit the while loop.

Hope this helps
Kevin smile.gif

This post has been edited by deery5000: 3 Jul, 2009 - 01:36 AM
User is offlineProfile CardPM
+Quote Post

Pipey85

RE: Why Is My Program Not Breaking The Loop?

3 Jul, 2009 - 02:39 AM
Post #3

New D.I.C Head
*

Joined: 30 Jun, 2009
Posts: 27

Thanks kevin, thats spot on biggrin.gif you earned yourself a thankyou.

The thought of using an extra break statement never crossed my mind, because I was trying to set the control statement to false, but as I found out it doesn't work tongue.gif

Thanks again Kevin smile.gif
User is offlineProfile CardPM
+Quote Post

deery5000

RE: Why Is My Program Not Breaking The Loop?

3 Jul, 2009 - 03:04 AM
Post #4

D.I.C Regular
***

Joined: 9 May, 2009
Posts: 354



Thanked: 20 times
My Contributions
Glad i coud help mate smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 08:12AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month