2 Replies - 192 Views - Last Post: 03 February 2018 - 11:32 PM Rate Topic: -----

#1 kianyt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 18

Need help with pretty basic computer

Posted 03 February 2018 - 10:12 PM

Code* not computer in title
hi, i'm trying to make a code that you enter a number, and then you enter any number except that number or you lose.
i know this is completely wrong, but i have no idea how to make it that if you put the same number twice you lose. Any help would be appreciated.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{   int wrongNumber;
    int tries;
    int number;
    cout << "Please enter any number" << endl;

while (tries<=100){
    cin >> number;
if (number == wrongNumber){
    cout << "Nice Job. You lost." << endl;}
    number == wrongNumber;
        cout << "Please enter any number except " << number << endl;

number = wrongNumber;


This post has been edited by kianyt: 03 February 2018 - 10:13 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with pretty basic computer

#2 tony jay  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: Need help with pretty basic computer

Posted 03 February 2018 - 11:21 PM

If you need to keep track of every number that is entered then you need a data structure, a vector would do, so that you can compare the most recently entered number with each of the numbers previously entered.
The way to do the comparison would be to use a for loop.

So, if I have understood correctly what should happen, do a little bit of research into vectors (they are part of the Standard Library) and the semantics of for loops.

Try to reduce the problem statement into several smaller parts that you will eventually map to one or more coding statements. This is called pseudocode, and is a very useful way of figuring out the flow of logic required.

Your while loop makes sense and the first couple of statements following it also make sense.
However, I am assuming that there needs to an an initial number (wrongnumber) that can be compared to number.
As you program stands wrongnumber is not initialised until AFTER the comparison with number in the if statement.
In fact, I assumed that you are really trying to assign wrongnumber with number but in actuality is a comparison statement.

Taking my advice, what you really want to do after getting number from the user is compare it whatever numbers are already in the vector (hint wrongnumber needs to be in the vector), and, if it is not found after comparing all the numbers in the vector, then that number is added to the vector and the loop proceeds to the next iteration and another number is entered.

If the number entered is the same as any of the previously entered numbers (these are in the vector) then that cout statement declaring one has lost needs to execute. After that, the way to escape the loop is to use the keyword 'break' followed by a semicolon to terminate the statement.

Try and put everything into pseudocode, and then, after one has looked up the basics of how for loops and vectors work try and code it.

You will note that I am trying to guide you to a solution without coding it for you.
The suggestions I have given you are useful exercises in themselves because no-one knows all the C++ there is to know and so all of us need to consult some reference or other at some points in time to either refresh ourselves on how something works, or, as in your case, to try and figure it out for the first time.
Also, the current problem you face is as much a logical problem as it is a coding problem (what C++ constructs can/should be used). Once the logic of the solution is plain for you then applying C++ code becomes easier.
Once you have successfully completed your current challenge you will have learned several very useful and important syntactical lessons in C++ as well some useful engineering techniques that hold whatever language you use to code.

Keep us posted...
Was This Post Helpful? 2
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Need help with pretty basic computer

Posted 03 February 2018 - 11:32 PM

Hi, here the syntax is not incorrect, and the compiler might not warn the programmer about it but surely the intended instruction is to assign a value.
16    number == wrongNumber;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1