| View previous topic :: View next topic |
| Author |
Message |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 3:36 pm Post subject: C++, "jumping" on a else? |
|
|
| Code: | #include "stdafx.h"
#include <iostream>
#include "Chasestest.h"
int main()
{
using namespace std;
cout << "Please enter your number: ";
cin >> x;
cout << "Good, now please enter the second number: ";
shorten();
}
void shorten()
{
using namespace std;
int y;
cin >> y;
int total;
total = x + y;
if (total == 42)
cout << " You have discovered the meaning of life!\n" << x + y;
else if (total <= 12)
cout << "\nWhy enter in such a low number\?\n";
else
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
} |
IS there anyway for me to only make it go to orange ONLY if the "Else" is triggered?
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Thu Aug 20, 2009 3:39 pm Post subject: |
|
|
yeah, use brackets { }.
| Code: | if (total == 42)
{
cout << " You have discovered the meaning of life!\n" << x + y;
}
else if (total <= 12)
{
cout << "\nWhy enter in such a low number\?\n";
}
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
} |
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 3:42 pm Post subject: |
|
|
| DanielG wrote: | yeah, use brackets { }.
| Code: | if (total == 42)
{
cout << " You have discovered the meaning of life!\n" << x + y;
}
else if (total <= 12)
{
cout << "\nWhy enter in such a low number\?\n";
}
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
} |
|
I get a compiler error after that?
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Thu Aug 20, 2009 3:44 pm Post subject: |
|
|
| well post whole code and compiler error.
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 3:45 pm Post subject: |
|
|
| Code: |
#include "stdafx.h"
#include <iostream>
#include "Chasestest.h"
int main()
{
using namespace std;
cout << "Please enter your number: ";
cin >> x;
cout << "Good, now please enter the second number: ";
shorten();
}
void shorten()
{
using namespace std;
int y;
cin >> y;
int total;
total = x + y;
if (total == 42)
{
cout << " You have discovered the meaning of life!\n" << x + y;
}
else if (total <= 12)
{
cout << "\nWhy enter in such a low number\?\n";
}
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
} |
1>------ Build started: Project: UNique, Configuration: Debug Win32 ------
1>Compiling...
1>UNique.cpp
1>c:\users\chase\documents\visual studio 2008\projects\unique\unique\unique.cpp(36) : fatal error C1075: end of file found before the left brace '{' at 'c:\users\chase\documents\visual studio 2008\projects\unique\unique\unique.cpp(15)' was matched
1>Build log was saved at "file://c:\Users\Chase\Documents\Visual Studio 2008\Projects\UNique\UNique\Debug\BuildLog.htm"
1>UNique - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I can't see anything wrong with the brackets.
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Thu Aug 20, 2009 3:47 pm Post subject: |
|
|
you forgot the last bracket.
| Code: | #include "stdafx.h"
#include <iostream>
#include "Chasestest.h"
int main()
{
using namespace std;
cout << "Please enter your number: ";
cin >> x;
cout << "Good, now please enter the second number: ";
shorten();
}
void shorten()
{
using namespace std;
int y;
cin >> y;
int total;
total = x + y;
if (total == 42)
{
cout << " You have discovered the meaning of life!\n" << x + y;
}
else if (total <= 12)
{
cout << "\nWhy enter in such a low number\?\n";
}
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
}
} |
ps. use indentation, you'll notice it when the brackets don't line up. for every opening bracket there must be a closing one.
Last edited by DanielG on Thu Aug 20, 2009 3:48 pm; edited 1 time in total |
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 3:48 pm Post subject: |
|
|
| DanielG wrote: | you forgot the last bracket.
| Code: | #include "stdafx.h"
#include <iostream>
#include "Chasestest.h"
int main()
{
using namespace std;
cout << "Please enter your number: ";
cin >> x;
cout << "Good, now please enter the second number: ";
shorten();
}
void shorten()
{
using namespace std;
int y;
cin >> y;
int total;
total = x + y;
if (total == 42)
{
cout << " You have discovered the meaning of life!\n" << x + y;
}
else if (total <= 12)
{
cout << "\nWhy enter in such a low number\?\n";
}
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
}
} |
|
Double brackets? hmm, looks like i need to read more on c++ lol
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Aug 20, 2009 4:02 pm Post subject: |
|
|
note if it's only one line you can leave out the brackets :
| Code: | if (total == 42)
{
cout << " You have discovered the meaning of life!\n" << x + y;
}
else if (total <= 12)
{
cout << "\nWhy enter in such a low number\?\n";
}
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
} |
could be :
| Code: | if (total == 42)
cout << " You have discovered the meaning of life!\n" << x + y;
else if (total <= 12)
cout << "\nWhy enter in such a low number\?\n";
else
{
cout << "\n You don't know the meaning of life? It's certainly not:\t\t\t" << total;
orange();
} |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Aug 20, 2009 8:39 pm Post subject: |
|
|
| Slugsnack wrote: | | note if it's only one line you can leave out the brackets[/code] |
1TBS or bust, in my opinion.
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Fri Aug 21, 2009 11:53 am Post subject: |
|
|
| i still hate K & R. i can only see it being useful for people with tiny screens trying to squish as much on as possible. with a nice big screen allman is the best, in my opinion
|
|
| Back to top |
|
 |
|