Hello, I'm trying to write a conditional IF statement for my program. What it's supposed to do is allow the user to enter numbers at random, but it cannot repeat any numbers.

#include <iostream>

using namespace std;


void main()
{
	
	int a;
	int b;
	int c;
	int d;
	int e;
	int f;
	int g;
		
	
	
	cout << "Enter Fist Number: ";
	cin >> a;
	//cout << a << "\n";
	cout << "Enter Second Number: "; 
	cin >> b;
	//cout << a << "\n";
	cout << "Enter Next Number: ";
	cin >> c;
	//cout << a << "\n";
	cout << "Enter Next Number: ";
	cin >> d;
	//cout << a << "\n";
	cout << "Enter Next Number: ";
	cin >> e;
	//cout << a << "\n";
	cout << "Enter Next Number: ";
	cin >> f;
	//cout << a << "\n";
	cout << "Enter Next Number: "; 
	cin >> g;
	//cout << a << "\n";
	if (a=b=c=d=e=f=g)
	{
		"Cannot Enter Same Numbers! ";

	}

}

Can someone please help me figure out how to do this. Thank you.

-Mike

The = is an assignment operator. Use == instead. Your code would still need some fixing, but I'm sure you can figure that out.

Are you allowed to use a vector? Or just an array?

Whenever you find yourself doing the same thing over and over and over, it is time to use a loop.

1. declare my vector or array or whatever
2. while not done
2.1. get a number from the user
2.2. if the number is not in the vector/array/whatever:
2.2.1. then add the number to the vector/array/...
2.2.2. else complain

Your posted if statement uses assignment operators, so it is the same as saying:

f=g;
e=f;
d=e;
c=d;
b=c;
a=b;
if (a) cout << "a is not zero.";
else cout << "a is zero.";

Make sure you use the == operator to test two values for equality:

int x, y;
x = 12;
y = 94;
if (x == y) cout << "impossible!";
else cout << "yeah!";

Hope this helps.

Are you allowed to use a vector? Or just an array?

Whenever you find yourself doing the same thing over and over and over, it is time to use a loop.

1. declare my vector or array or whatever
2. while not done
2.1. get a number from the user
2.2. if the number is not in the vector/array/whatever:
2.2.1. then add the number to the vector/array/...
2.2.2. else complain

Your posted if statement uses assignment operators, so it is the same as saying:

f=g;
e=f;
d=e;
c=d;
b=c;
a=b;
if (a) cout << "a is not zero.";
else cout << "a is zero.";

Make sure you use the == operator to test two values for equality:

int x, y;
x = 12;
y = 94;
if (x == y) cout << "impossible!";
else cout << "yeah!";

Hope this helps.

I've only been coding for about 2 weeks now. :-) So I'm trying to understand. You want to assign the variables to equal each other?

But if I am trying to make it so the user inputs the numbers, wouldn't that not work?

Basically what I'm shooting towards it to make it so my program will tell them "You cannot enter the same number twice" then re-loop them to the mistake.

Yes, I understand that.

What you did is assign all the variables to have the same value as g, because you used = instead of == .

Is this schoolwork? If so, have you learned about STL vectors, or arrays? You will need one of them to keep track of which numbers have been entered already.

No, we haven't learned those...Just started. What are those?

I must be stupid.

Ah, ye old double-post button. Since I'm not allowed to edit anything I post after a couple minutes I'll just add a new post:

You'll have to go ask your professor how he expects you to remember whether a number was inputed previously or not. There is no simple, straightforward way to do it without at least an array or a linked list.

Let me know what you want to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.