Trying to transfer this Psuedocode into actual code having a bit of trouble

Write your question here.


//Start
// Declarations
// number numberToGuess
// number myGuess;
//
// numberToGuess = 92
// while myGuess != numberToGuess
// output "Please guess an integer number between 1 and 100"
// input myGuess
// if (myGuess == numberToGuess)
// output "You guessed the correct number"
// else
// output "The number you guessed was inccorect. Try again! "
// end if
// end while
// output "Thanks for playing the guessing game. Have a great day!"
//Stop
Yea you are, you haven't started. I guess we better get started... This will help.

1
2
3
4
5
6
7
8
//Start
int main()
{


//Stop
return 0;
}
Last edited on
Yeah just started a class teaching C++ as the first language and having tons of trouble trying to learn everything i got the starting point just not entirely sure where to go from there or even where i need to put things.
//PLD #6 pg. 122
#include <iostream>

int main ()
{
number numberToGuess
number myGuess;

numberToGuess = 92;
while myGuess != numberToGuess
std::cout << "Please guess and integer number between 1 and 100"
input myGuess
}
if (myGuess == numberToGuess)
{
std::cout << "You guessed the correct number"
else;
std::cout << "The number you guessed was incorrect. Try again!"
endif
}
end while
{
std::cout << "Thanks for playing the guessing game. Have a great day!"
}
stop




so far this is what i actually have
int numberToGuess;

number isn't a built in type. There are a bunch of built in types, but for anything other than (int and char) I prefer to use the c99 types which explicitly list the # of bits used (because the other names can change the # of bits across some platforms)


while(expression) //you need the () on these statements, and ifs, etc.

brackets align to code blocks

main
{
stuff in main
while
{
stuff in the while;
}
more stuff in main
}

else; is a wasted statement. ; means "do nothing" or "end of statement".
if(expression); means "if expression, do nothing"
else; means do nothing. These are common mistakes, you do not want the ;s..
looks like below without the ;s... all normal statements have a ; on the end, but loops, conditions, etc do NOT.

endif is not a thing. all code blocks begin and end with {} brackets.
looks like:
if (whatever)
{
stuff;
}
else
{
other stuff;
}

If you do not use {} after loop or conditional statements, the very first statement ONLY is inside. I prefer to always use {} even for 1 line in professional code. Messing around, I might leave it off.

stop isn't a thing. return 0; tells the MAIN function which is of type int to return a 0 to the operating system which tells the operating system that the program exited normally. Other codes returned indicate an error, defined by the program that failed. This is not used much anymore; most errors are trapped and handled rather than failed out this way (professionally) but classroom stuff may use it.

cout << string << endl; // <--- end of lines are helpful for the program's output to look correct.

cin is the opposite.
cin >> guess; //reads from the user's typed input...



see what you can do armed with this info...
Last edited on
To apply what jonnin said, this is what your code would look like in c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
int myGuess;
int numberToGuess = 92;

while(myGuess != numberToGuess)
{
cout << "Please guess a number between 1 and 100" << endl;
cin >> myGuess

if(myGuess == numberToGuess)
{
cout << "You guessed the correct number" << endl;
}
else
{
cout << "The number you guessed was incorrect.  Try again!" << endl;
}
}
cout << "Thanks for playing the guessing game.  Have a great day!" << endl;
system("pause");
return 0;
}

Last edited on
Thank you all this really helped!
myGuess is declared but not initialized (line 6), so it's not 100% certain that it is != 92 upon declaration. Therefore best to initialize myGuess upon declaration to 0 or some other specific number
Topic archived. No new replies allowed.