|
|||||||
| Forum Home | Register | Members List | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | ||||||||
|
Administrator
Location: UK |
The latter is 'better' but I have no idea whether they will. compile out to be the same
__________________
Steven Yau [Alix Games Blog] [Portfolio] [How I broke into the Games Industry] [Why I left my Games Job] [How to be a Games Tester] [Getting back into the Game] |
||||||||
|
|
|
|
|
#12 | |||||||||
|
Administrator
Location: UK |
Quote:
__________________
Steven Yau [Alix Games Blog] [Portfolio] [How I broke into the Games Industry] [Why I left my Games Job] [How to be a Games Tester] [Getting back into the Game] |
|||||||||
|
|
|
|
|
#13 | ||||||||
|
Member
|
|
||||||||
|
|
|
|
|
#14 | ||||||||
|
Moderator
Location: Netherlands |
Well, it would be a simple rule. Check if a function is called sequentially, see if the calls can be combined, combine if so. Compilers can do pretty freaky shit. I wouldn't be surprised if it is implemented somewhere. (-O6?
)
__________________
I'm a web developer by profession, but a game developer by heart. Uh oh! The princess is in another signature! |
||||||||
|
|
|
|
|
#15 | |||||||||
|
Administrator
Location: London, UK |
Quote:
For example, the following two code extracts produced the same bytecode: Code:
String str = "a" + "b" + "c" + "d" + "e"; Code:
String str = "a"; str += "b"; str += "c"; str += "d"; str += "e"; Code:
System.out.print("a" + "b" + "c" + "d" + "e");
Code:
System.out.print("a");
System.out.print("b");
System.out.print("c");
System.out.print("d");
System.out.print("e");
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education Networking | Research | Teaching |
|||||||||
|
|
|
|
|
#16 | |||||||||
|
Moderator
Location: Netherlands |
Interesting stuff! A bit scary, though. You can't find this sort of stuff in documentation.
Quote:
So the question rises: is the first method very inefficient, or is the second simply very efficiently resolved? ![]()
__________________
I'm a web developer by profession, but a game developer by heart. Uh oh! The princess is in another signature! |
|||||||||
|
|
|
|
|
#17 | ||||||||
|
Junior Member
Location: Martinsville, IN |
nef,
I took into account all the stuff you have said. I've been taking a break from learning C++ to work on my resume and school work, so I just got around to rewriting today. Code:
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using std::cout; //Load cout from namespace std - A standard output stream object
using std::cin; //Load cin from namespace std - A standard input stream object
int QuestionOne()
{
int answer; //Initialize "answer" variable.
int answer1; //Initialize "answer1" variable.
int answer2; //Initialize "answer2" variable.
int answer3; //Initialize "answer3" variable.
int answer4; //Initialize "answer4" variable.
answer1 = 1; //Set "answer1" variable to 1.
answer2 = 2; //Set "answer2" variable to 2.
answer3 = 3; //Set "answer3" variable to 3.
answer4 = 4; //Set "answer4" variable to 4.
cout <<"What is the programmer's full name?\n"
"1.) Matthew Benjamin Durpo\n"
"2.) David Matthew Burpo\n"
"3.) Matthew David Burpo\n"
"4.) Benjamin Matthew Durpo\n"
"\n"
"Answer: "; //Display all strings between cout and ; on screen.
cin >> answer; //Answer input.
cin.ignore(); //Ignore a single character and allow prompt to continue.
if (answer == answer3) //If correct, do this function.
{
cout <<"\n"
"That's correct!\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
else //If incorrect, do this function.
{
cout <<"\n"
"Incorrect answer! The programmer is Matthew David Burpo.\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
return(0);
}
int QuestionTwo()
{
int answer; //Initialize "answer" variable.
int correctanswer;
correctanswer = 24; //Assign "answer" variable to 24.
cout <<"How old is Matthew?\n"
"\n"
"Answer: "; //Display all strings between cout and ; on screen.
cin >>answer; //Answer input.
cin.ignore(); // Ignore a single character and allow prompt to continue.
if (answer == correctanswer) //If correct, do this function.
{
cout <<"\n"
"That's Correct!\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
else //If incorrect, do this function.
{
cout <<"\n"
"Incorrect answer! Matthew is 24 years old.\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
return (0);
}
int QuestionThree()
{
int answer; //Initialize "answer" variable.
int answer1; //Initialize "answer1" variable.
int answer2; //Initialize "answer2" variable.
int answer3; //Initialize "answer3" variable.
int answer4; //Initialize "answer4" variable.
int answer5; //Initialize "answer5" variable.
int answer6; //Initialize "answer6" variable.
int answer7; //Initialize "answer7" variable.
answer1 = 1; //Set "answer1" variable to 1.
answer2 = 2; //Set "answer2" variable to 2.
answer3 = 3; //Set "answer3" variable to 3.
answer4 = 4; //Set "answer4" variable to 4.
answer5 = 5; //Set "answer5" variable to 5.
answer6 = 6; //Set "answer6" variable to 6.
answer7 = 7; //Set "answer7" variable to 7.
cout <<"Matthew wants to be a:\n"
"1.) Video Game Designer\n"
"2.) Video Game Programmer\n"
"3.) Video Game Producer\n"
"4.) Both 1 & 2\n"
"5.) Both 1 & 3\n"
"6.) Both 2 & 3\n"
"7.) 1, 2, & 3\n"
"\n"
"Answer: "; //Display all strings between cout and ; on screen.
cin >>answer; //Answer input.
cin.ignore(); // Ignore a single character and allow prompt to continue.
if (answer == answer4) //If correct, do this function.
{
cout <<"\n"
"That's correct!\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
else //If incorrect, do this function.
{
cout <<"\n"
"Incorrect answer! Matthew wants to be a video game programmer and designer.\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
return (0);
}
int QuestionFour()
{
int answer; //Initialize "answer" variable.
int answer1; //Initialize "answer1" variable.
int answer2; //Initialize "answer2" variable.
int answer3; //Initialize "answer3" variable.
int answer4; //Initialize "answer4" variable.
answer1 = 1; //Set "answer1" variable to 1.
answer2 = 2; //Set "answer2" variable to 2.
answer3 = 3; //Set "answer3" variable to 3.
answer4 = 4; //Set "answer4" variable to 4.
cout <<"What school is Matthew attending?\n"
"1.) Ivy Tech\n"
"2.) ITT Tech\n"
"3.) DeVry\n"
"4.) University of Phoenix\n"
"\n"
"Answer: "; //Display all strings between cout and ; on screen.
cin >>answer; //Answer input.
cin.ignore(); // Ignore a single character and allow prompt to continue.
if (answer == answer2) //If correct, do this function.
{
cout <<"\n"
"That's correct!\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
else //If incorrect, do this function.
{
cout <<"\n"
"Incorrect answer! Matthew is attending ITT Tech.\n"
"\n"
"Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
}
return(0);
}
int main() //Start program - Entry point of main function.
{
cout <<"Welcome to the quiz.\n"
"Please, enter the number corresponding to the correct answer.\n"
"Press 'Enter' to begin.\n"
"\n"; //Display all strings between cout and ; on screen.
cin.get(); //Press Enter Key to execute next command.
int AskQuestion;
AskQuestion = QuestionOne();
cout << AskQuestion;
AskQuestion = QuestionTwo();
cout << AskQuestion;
AskQuestion = QuestionThree();
cout << AskQuestion;
AskQuestion = QuestionFour();
cout << AskQuestion;
AskQuestion = QuestionFive();
cout << AskQuestion;
AskQuestion = QuestionSix();
cout << AskQuestion;
AskQuestion = QuestionSeven();
cout << AskQuestion;
cout <<"Press 'Enter' to exit the program...\n"; //Display on screen.
cin.get(); //Press Enter Key to exit the program.
return 0; //End program - Normal termination of main function.
}
|
||||||||
|
|
|
|
|
#18 | ||||||||
|
Junior Member
Location: Martinsville, IN |
Forgot to mention I'm having problems in there with the returns. It's returning 0 as an output on screen where it says "0Question text here"
|
||||||||
|
|
|
|
|
#19 | |||||||||
|
Senior Member
Location: London |
Quote:
Code:
AskQuestion = QuestionOne();
cout << AskQuestion;
In other words, change the above code to: Code:
QuestionOne(); QuestionTwo(); QuestionThree(); ..... |
|||||||||
|
|
|
|
|
#20 | ||||||||
|
Moderator
Location: Netherlands |
You know you can define and initialize variables at the same time, right?
Code:
int answer; answer1 = 1; Code:
int answer1 = 1; ![]()
__________________
I'm a web developer by profession, but a game developer by heart. Uh oh! The princess is in another signature! |
||||||||
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 04:59 PM.
































Linear Mode

