#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);
}
N00b N00b N00b help the N00b
ok,ok, here is what i have been working on.....
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?
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:
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}
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);
}
#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);
}
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 you want to execute more than one line in an if/else statement you do like this:
this is also true for do/while, for, and any other control statement
hope that helps !
Matt
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 statementcout << "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
This code here is modified for you:
Here is an example of what you are trying to achieve with explainations:
Just use the 'endl' in the first code I posted above... not really much to explain. 'endl' = "endline"
#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"
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";
*** SPOILERS AHEAD ***
Beat the final boss by pressing "5";
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??
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement