Jump to content
  • Advertisement
Sign in to follow this  
Haku

N00b N00b N00b help the N00b

This topic is 5401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok,ok, here is what i have been working on.....
#include <iostream>
#include <ostream>
#include <istream>
#include <stdlib.H>

using std::cin;
using std::cout;

int main ()
{
int x=5;
int i;

cout << "enter the number five";
cin >> i;
if (i==x)
cout << "well done!";
else
cout << "i said type five dumbass";
system("PAUSE");
return(0);
}


first thing,i have done an if statement. After the else i want to do another if statement but i don't want this if statement to happen under the else section. How do i seperate it so it is not part of the else line? second thing, i have added 'system("pause") and i want it to be on a new line than my cout << ""; when i put '\n' it doesn't work, why not?

Share this post


Link to post
Share on other sites
Advertisement
try this
#include <iostream>
#include <ostream>
#include <istream>
#include <stdlib.H>

using std::cin;
using std::cout;

int main ()
{
int x=5;
int i;

cout << "enter the number five";
cin >> i;
if (i==x)
{
cout << "well done!";
else
cout << "i said type five dumbass";
};
if(//case)
{
//whatever you want to go here
};
system("PAUSE");
return(0);
}

Share this post


Link to post
Share on other sites
First, name your post something relevant. All the 'noob's are getting annoying to read.

Anyway, as for if, else's I'm not totally clear on what you're asking. Regardless:

int x = 1;
if (x==1){
// the code goes here
}
else {
// the code does not go here
}
if(x==5) {
// this is seperate from the above if/else match
}

Share this post


Link to post
Share on other sites
when you don't put { and } after if or else only the next line is part of the if or else statement like this:

if(i == 2)
cout << "this is printed only if i == 2" << endl;
else
cout << this is printed only if i != 2" << endl;
// whatever you put after this will be executed outisde of the if/else statement
cout << "This line is always printed" << endl;



if you want to execute more than one line in an if/else statement you do like this:

if(i == 2)
{
cout << "this is printed";
cout << "only if i == 2" << endl;
}
else
cout << "this is printed only if i != 2" << endl;
cout << "this is always printed" << endl;



this is also true for do/while, for, and any other control statement

hope that helps !
Matt

Share this post


Link to post
Share on other sites
This code here is modified for you:


#include <iostream>
#include <ostream>
#include <istream>
#include <stdlib.H>

using std::cin;
using std::cout;

int main ()
{
int x=5;
int i;

cout << "enter the number five";
cin >> i;
if (i==x)
cout << "well done!" << endl; //*CHANGED*
system("PAUSE"); //*CHANGED*
else
{
cout << "i said type five dumbass" << endl; //*CHANGED*
system("PAUSE");
}

return(0);
}



Quote:
first thing,i have done an if statement. After the else i want to do another if statement but i don't want this if statement to happen under the else section. How do i seperate it so it is not part of the else line?


Here is an example of what you are trying to achieve with explainations:

/***************if statement #1***********************/
int a = 1;
int b = 0;

if (a=1)
b = a; /* Here you dont need any opening and closing braces
because the if statement only processess one line
of code */


/***************if statement #2***********************/
int a = 1;
int b = 0;

if (a=1)
{
a = a + 1;
b = a
} /* Here you do need opening and closing braces because the
if statement processes more than one line of code */


/***************if/else statement***********************/
int a = 1;
int b = 0;

if (a=1)
{
a = a + 1;
b = a
}
else
cout << "a != 1"; /* Notice i didnt use any opening and
closing braces for this else statement.
I'll leave it to you to figure why ;) */








Quote:
second thing, i have added 'system("pause") and i want it to be on a new line than my cout << ""; when i put '\n' it doesn't work, why not?


Just use the 'endl' in the first code I posted above... not really much to explain. 'endl' = "endline"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hmm, that game wasn't very hard at all. I managed to beat it in only 30 hours!

*** SPOILERS AHEAD ***

Beat the final boss by pressing "5";

Share this post


Link to post
Share on other sites
Good lord, someone asks a question on one of the most basic parts of the C syntax and gets two responses that are not only wrong, but wouldn't even compile??

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!