|
 |
|
 |
05-24-2005, 05:22 PM
|
#31 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
An important question. Can the switch::case call other functions, and is it the same format...
case 1 main();
case 2 menu1();
etc...
?
|
|
|
05-24-2005, 10:30 PM
|
#32 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
yes..
Code:
#include<iostream>
void stars(){
for (int x = 0 ; x <= 40; x++)
cout << "*";
cout << endl;
}
void bang(){
for (int x = 0 ; x <= 40; x++)
cout << "!";
cout << endl;
}
int main()
{
int ask;
cout << " enter a choice (1) for stars (2) for bangs: " ;
cin >> ask;
switch (ask)
{
case 1:
stars(); //calls star function
break;
case 2:
bang(); //calls bang function
break;
default: //incase no input matches
cout << "you did not enter a correct input only 1 or 2 is accepted" << endl;
}
return 0;
}
|
|
|
05-25-2005, 03:40 PM
|
#33 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Thanks! I messed up the syntax a little, but that example set me straight.
|
|
|
05-25-2005, 05:31 PM
|
#34 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Another quick question, does the case have to follow sequence?
case 1, case 2, case 3...
or can I do:
case 1, case 2, case 3, case 0?
and can I even use a zero?
|
|
|
05-25-2005, 05:40 PM
|
#35 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
Quote:
|
Originally Posted by cheawick
Another quick question, does the case have to follow sequence?
case 1, case 2, case 3...
or can I do:
case 1, case 2, case 3, case 0?
and can I even use a zero?
|
it does not have to be in order, but the code will be faster if it is..
a good site
http://www.cplusplus.com/doc/tutorial/index.html
|
|
|
05-25-2005, 09:59 PM
|
#36 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
it does not have to be in order, but the code will be faster if it is..
|
Who told you this? This is wrong.
You may want to put the most frequent occurring event as the first case; the second most frequent event as the second case, etcetera. Especially embedded systems will benefit from this approach. As usual, only tests can show if this applies to your program. However, the standard (ISO) doesn't dictate performance increases. So besides testing, you'll need to study the behaviour of your compiler as well.
__________________
|
|
|
05-26-2005, 04:52 AM
|
#37 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
Quote:
|
Who told you this? This is wrong.
|
got it from C++ how to program fourth edition H.M. Deitel. under the part where it talks about speeding up If staments by using inorder (assending or decending) case statments.
so I may have missunderstood what they were talking about.
|
|
|
05-26-2005, 08:05 AM
|
#38 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Or deitel didn't get it either. Don't think writers are ISO C++ experts. They aren't in most cases.
If relevant, just put the most frequent occurances on top of switch statements. You even may be better off to nest switch statements, where the nested switch is the least frequent occurence!
Basically, all you want to achieve is to prevent false evaluations before true evaluations, because compilers will generate code of such nature that the code will require extra calculation to jump to the right table for the next evaluation in this case.
__________________
|
|
|
05-27-2005, 03:59 PM
|
#39 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Thanks ya'll. I was just planning on using the zero as the exit/return to previous menu option. I promise to post the code soon, our inspection at work went well, so I will have more time to work on this. At least until the end of July. Then I'll be gone for about a month or so.
|
|
|
05-30-2005, 05:29 PM
|
#40 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Need a little help to find a parse error. Compiler didn't give me a line number.
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
void testmenu()
void editmenu()
void main()
{
int selectm;
cout<<"TestMaker v0.1";
printf("By Chaos\n\n\n\n");
cout<<"Please select the following:";
printf("\n\n");
cout<<"1 Test Menu";
cout<<"2 Edit Menu";
cout<<"3 Options";
printf("\n0 Exit Program");
printf("\nPlease enter a menu number:\n");
getchar(); //prog flow test
cin>>selectm;
switch(selectm)
{
case 1:
testmenu();
break;
case 2:
editmenu();
break;
/*
case 3:
options(); //might get rid of this
//looks like it will be easier to incorporate
//in each submenu
break;
*/
case 0:
//confirm program exit
//return to MAIN or exit
default: //incase input doesn't match
cout<<"INVALID MENU NUMBER"<<endl;
}
return 0;
}
void testmenu()
{
int selectt;
printf("TEST MENU:\n\n\n\n");
cout<<"Please select the following:";
cout<<"1 Flash Cards";
cout<<"2 Take a Test";
cout<<"3 Take a Multisubject Test";
printf("\n0 Return to Main Menu");
getchar(); //prog flow check
cin>>selectt;
switch(selectt)
{
case 1: //flash cards
//display subject list
//select subject for flashcards
//randomize questions (Y/N)
//open subject file
//display question and correct answer only
//on end ask if user wishes to review questions or return to
//TEST MENU
break;
case 2:
test();
break;
case 3:
multitest();
break;
case 0:
main();
break;
}
}
void editmenu()
{
int selecte;
printf("EDIT MENU:\n\n\n\n");
cout<<"Please select from the following:";
cout<<"1 Create a new subject";
cout<<"2 Edit a subject question database";
cout<<"3 Delete a Subject";
printf("/n0 Previous Menu");
getchar(); //prog flow check
cin>>selecte;
switch(selecte)
{
case 1:
//get name of subject
//save file
//return to EDIT MENU or go to case 2 if possible
break;
case 2:
//display list of subjects
//enter subject name
//load subject file
//allow viewing, adding, deleting, and editing of Q&A
//save file
//return to EDIT MENU
break;
case 3:
//display list of subjects
//enter subject name
//confirm subject name
//delete file
//return to EDIT MENU
break;
case 0:
main();
break;
}
}
|
|
|
05-30-2005, 06:54 PM
|
#41 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
for starters
you need after your funtion prototypes.
example you have while that should be also is this a c or c++ program.. I guess c++ with the // for coments
if its c++ its not required, but is more cross compiler frinedly to use the C++ libs like #include<cstdlib> insted of #include<stdlib.h>
you also have call to libaryars that are not used. iostream is the only one I see needed so far.
also some compilers do not like void main()
Dev-c++ throws an error, along with gcc with the error: `main' must return `int'
also you have call to funtions that do not exsist.. this should throw a linker error (see test and multitest)
this compiles for me with these changes.
using Dev-C++
you can get this compiler for free as its Open Source.
http://www.bloodshed.net/devcpp.html
Code:
#include <iostream>
#include <cstdlib>
//#include <conio>
//#include <stdio>
using namespace std;
void testmenu();
void editmenu();
void test();
void multitest();
int main()
{
int selectm;
cout<<"TestMaker v0.1";
printf("By Chaos\n\n\n\n");
cout<<"Please select the following:";
printf("\n\n");
cout<<"1 Test Menu";
cout<<"2 Edit Menu";
cout<<"3 Options";
printf("\n0 Exit Program");
printf("\nPlease enter a menu number:\n");
getchar(); //prog flow test
cin>>selectm;
switch(selectm)
{
case 1:
testmenu();
break;
case 2:
editmenu();
break;
/*
case 3:
options(); //might get rid of this
//looks like it will be easier to incorporate
//in each submenu
break;
*/
case 0:
//confirm program exit
//return to MAIN or exit
default: //incase input doesn't match
cout<<"INVALID MENU NUMBER"<<endl;
}
return 0;
}
void testmenu()
{
int selectt;
printf("TEST MENU:\n\n\n\n");
cout<<"Please select the following:";
cout<<"1 Flash Cards";
cout<<"2 Take a Test";
cout<<"3 Take a Multisubject Test";
printf("\n0 Return to Main Menu");
getchar(); //prog flow check
cin>>selectt;
switch(selectt)
{
case 1: //flash cards
//display subject list
//select subject for flashcards
//randomize questions (Y/N)
//open subject file
//display question and correct answer only
//on end ask if user wishes to review questions or return to
//TEST MENU
break;
case 2:
test();
break;
case 3:
multitest();
break;
case 0:
main();
break;
}
}
void editmenu()
{
int selecte;
printf("EDIT MENU:\n\n\n\n");
cout<<"Please select from the following:";
cout<<"1 Create a new subject";
cout<<"2 Edit a subject question database";
cout<<"3 Delete a Subject";
printf("/n0 Previous Menu");
getchar(); //prog flow check
cin>>selecte;
switch(selecte)
{
case 1:
//get name of subject
//save file
//return to EDIT MENU or go to case 2 if possible
break;
case 2:
//display list of subjects
//enter subject name
//load subject file
//allow viewing, adding, deleting, and editing of Q&A
//save file
//return to EDIT MENU
break;
case 3:
//display list of subjects
//enter subject name
//confirm subject name
//delete file
//return to EDIT MENU
break;
case 0:
main();
break;
}
}
void test(){}
void multitest(){}
|
|
|
05-30-2005, 07:13 PM
|
#42 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Thanks iccaros! Yes, I wish to make this a c++ program. And yes also to wanting to make this a cross compiler friendly program, so all adivce concerning this is very appreciated. I do also use the DevC++ compiler from bloodshed. I am unfamiliar with the <cstdlib>, what is this? Also the extra includes will be implemented with the clear screen code that I plan to add after things work. I believe that I posted that portion in page two of this thread, I think. I will look, but post this incase the post may be lost.
|
|
|
05-30-2005, 07:14 PM
|
#43 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Nope, it was at the end of page one here.
|
|
|
05-30-2005, 07:24 PM
|
#44 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
Include the standard header <cstdlib> to effectively include the standard header <stdlib.h> within the std namespace.
in other words..its the c++ version of <stdlib.h>
both dev-c++ and gcc throw an error on <stdlib.h>
http://msdn.microsoft.com/library/de...lib_header.asp
|
|
|
05-30-2005, 07:32 PM
|
#45 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
here is my standard clear screen funtion.. it works well for funtion calls. in dev-c++ you must go to execute --> perameter and enter WIN32 or for Gcc on a Unix system it the -D option example g++ foo.cpp -DUNIX
void clear_screen()
{
#if defined WIN32
system("cls");
#elif defined UNIX
system("clear");
#endif
}
if someone knows a better way wiht out system calls for consouls I'm all ears.
also I believe you should have <cstdlib> for this .
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:42 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|