Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 05-24-2005, 05:22 PM   #31 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
An important question. Can the switch::case call other functions, and is it the same format...
case 1 main();
case 2 menu1();
etc...
?
cheawick is offline   Reply With Quote
Old 05-24-2005, 10:30 PM   #32 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
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;
}
iccaros is offline   Reply With Quote
Old 05-25-2005, 03:40 PM   #33 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
Thanks! I messed up the syntax a little, but that example set me straight.
cheawick is offline   Reply With Quote
Old 05-25-2005, 05:31 PM   #34 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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?
cheawick is offline   Reply With Quote
Old 05-25-2005, 05:40 PM   #35 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
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
iccaros is offline   Reply With Quote
Old 05-25-2005, 09:59 PM   #36 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 05-26-2005, 04:52 AM   #37 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
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.
iccaros is offline   Reply With Quote
Old 05-26-2005, 08:05 AM   #38 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 05-27-2005, 03:59 PM   #39 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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.
cheawick is offline   Reply With Quote
Old 05-30-2005, 05:29 PM   #40 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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;
}
}
cheawick is offline   Reply With Quote
Old 05-30-2005, 06:54 PM   #41 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
for starters
you need
Code:
 ;
after your funtion prototypes.

example you have
Code:
 void testmenu()
while that should be
Code:
  void testmenu();
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(){}
iccaros is offline   Reply With Quote
Old 05-30-2005, 07:13 PM   #42 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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.
cheawick is offline   Reply With Quote
Old 05-30-2005, 07:14 PM   #43 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
Nope, it was at the end of page one here.
cheawick is offline   Reply With Quote
Old 05-30-2005, 07:24 PM   #44 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
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
iccaros is offline   Reply With Quote
Old 05-30-2005, 07:32 PM   #45 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
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 .
iccaros is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java/jsp test question ideas sde Java 2 03-25-2005 07:38 PM
How to test for empty date from MySql call? Wysocki PHP 3 02-06-2005 02:17 PM
how to test PHP R0B PHP 3 10-27-2004 03:28 PM


All times are GMT -8. The time now is 07:42 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting