LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2010, 09:08 AM   #1
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Rep: Reputation: 44
c++ loop windows


Hi guys here is my code
Code:
#include <iostream>
using namespace std;


int guess;
int number = 5;
int tries = 3;



void main()
{

    cout << "Guess The Number In 3 Tries ";
    cin >> guess;


    if (guess != number)
    {
        cout << "Wrong";        
        tries = tries - 1;
        cout << "you have " << tries << " tries left" << endl;
        
        

        if (tries = 0)
        {
            cout << "0 tries Remaining goodbye";
            exit (0);
        
        }
    }

    else 
    {
        cout << "Correct";
    }
}
i just cant seem to understand loops.

i havent put the loop in yet but what i would like is the user only to have 3 tries at guessing number then exit if wrong or it says "correct if right"

tbh i dont even know if the above code is correct. i have tried several ways and position of the loops but each time is comes out wrong

can anyone give me any pointers

Thanks in Advance
 
Old 10-03-2010, 09:15 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Well, try adding your loop and repost the code.
Code:
 if (tries = 0)
That code will always be true, because you only used one equals sign. Which means you are assigning '0' to the variable 'tries', which succeeds, so the statement becomes "if (1)" where 1 is aka 'true'. Use a double equals to compare.

You're also going to want to add newlines to your couts, for readability.
Code:
 cout << "Correct" << endl;
// OR
 cout << "Correct\n";
 
Old 10-03-2010, 09:19 AM   #3
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Original Poster
Rep: Reputation: 44
Hi thank you for the quick reply.

sorry i didnt add any loops because i didnt really know which loop to put in the code hence the post. TBH i dont want someone to actually tell me teh answer for the code but just some pointers as in what loop should i use and roughly where about in the code it should go .

Thanks for teh == i forgot about that. readability aint really a problem at moment as its not for work uni etc etc its just little old me practicing lol

Last edited by pr0xibus; 10-03-2010 at 09:21 AM.
 
Old 10-03-2010, 09:22 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by pr0xibus View Post
...
i just cant seem to understand loops.
...
Then probably start from 'goto': http://en.wikipedia.org/wiki/Goto ; http://www.java2s.com/Tutorial/Cpp/0...oopexample.htm .
 
Old 10-03-2010, 09:27 AM   #5
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
I would use a while loop and restructure your code a little. It's hard to do this without telling you the answer..

A little more concretely, in the while loop's opening statement I would compare "tries" to 0 - the idea is to only loop while tries is greater than 0.

Plus, I would make the program exit with success if the number is guessed, and always exit with failure after the while loop - because once you have the loop down, if you get out of it, it means the user has failed to guess. This means you don't have to compare "tries == 0" anywhere.
 
Old 10-03-2010, 09:36 AM   #6
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Original Poster
Rep: Reputation: 44
hi guys

i know its hard without actually telling me, but its something i want to how can i put it figure out mostly by myself and with only having internet access for a few hours of the day its kind of hard too look for samples etc etc. TBH today is teh first time i have used c++ i used delphi about 4 years ago and nothing since then lol
 
Old 10-03-2010, 09:37 AM   #7
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Original Poster
Rep: Reputation: 44
hi guys

i know its hard without actually telling me, but its something i want to how can i put it figure out mostly by myself and with only having internet access for a few hours of the day its kind of hard too look for samples etc etc. TBH today is teh first time i have used c++ i used delphi about 4 years ago and nothing since then lol
 
Old 10-03-2010, 12:31 PM   #8
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Original Poster
Rep: Reputation: 44
cheers for teh help guys managed to get it sorted

Code:
#include <iostream>
using namespace std;


int guess;
int number = 5;
int tries = 3;



void main()
{

    while (tries > 0)
    {
        cout << "Guess The Number In " << tries << " Tries " << endl;
        cin >> guess;
        tries = tries - 1;

        if (guess != number)
        {
            cout << " Incorrect number ";
            cout << " You Have " << tries << " Left" << endl;
        }

        if (guess == number)
        {

            cout << " Correct Number ";
            exit (0);
        }

        if (tries == 0)
        {
            cout << " Sorry 3rd attempt Failed " << endl;
            system("pause");
            exit (0);
        }
    }


}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Windows boot loop... kevin63661 Linux - Hardware 10 08-12-2009 10:52 PM
GRUB in infinite loop after Windows XP install mvmannheim Linux - Software 5 03-06-2009 05:14 PM
an wget with proxy that can download in loop for windows XP? frenchn00b General 0 07-14-2008 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration