Multi-lingual, random output-Hello World program

Hello guys! absolute beginner here. trying to write a program that randomly displays one of these 6 phrases -

English Hello world!
Spanish Hola mundo!
Latin: Salve mundi!
German: Hallo Welt!
Chinese: Ni hao shijie!
Arabic: Maraba allam!

i figured i would use rand(), and limit it to the numbers 1 - 6, and then, using if statements, decide on which statement to display.

please help me out here, its not working out as i had planned


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()

{

unsigned seed = time(0);

srand(seed);

int x;

x = (1 + rand() % 6); // limits random numbers to 1 - 6

if (x = 1){
cout << "Hello world!";}

else if (x = 2) {
cout << "Hola mundo!";}

else if (x = 3) {
cout << "Salve mundi!";}

else if (x = 4) {
cout << "Hallo Welt!";}

else if (x = 5) {
cout << "Ni hao shijie!";}

else (x = 6); {
cout << "Maraba allam!";}

system("PAUSE");

return 0;

}


it displays "hello world" & "maraba allam" and im guessing thats because '1' & '6' are in the declaration line of x


any tips??????
you're using the assignment operator if ( x = 1 ) not the comparison operator if ( x == 1 )
DON'T USE SYSTEM PAUSE!!!! Use something like std::cin instead. System pause is platform specific, and lowers people's opinion of your coding ability.
the course instructor specifically wants us to use system pause, its a beginning course. sorry man! lol i knew i was gonna catch some shit for that.

@quirkyusername thank you! it is working now but, any idea as to why it is also always displaying Maraba Allam?
NVM! i just changed the last else to else if.

i was under the impression the last statement in an "if series" always had to be just else...
You can at least ask the instructor the reason why they're so resolute on teaching students sloppy habits.

More on topic, notice that else just means else, it can't have a condition attached to it. So this:

1
2
else (x == 6); {
cout << "Maraba allam!";}

particularly because you added a semicolon to the first line, is read as:

1
2
3
4
else {
    x == 6;
}
cout << "Maraba allam!";

which is why the last line is always printed.
Last edited on
because it is else (x = 6) not else (x == 6) ok?
Wouldn't a switch case be better. Or does the "instructor" want you to use "if" statements...
Topic archived. No new replies allowed.