GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Feb-2004, 11:52
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road

C switch / loop question -- Was: Me again.


I've got the memory allocation i think, now its down to what should be the simple stuff. I think I've been on it too long and all it needs is new eyes.

the menu (switch clause) should loop forever till i choose the option to quit. but its not, i can choose a function once and it will go back to the menu clause, but if i enter a second function it then decides it wants to close. I cant see why, mucho frustrating.

Any help would be appreciated as always, and hopefully one day i'll be answering the questions.

Mitch
Attached Files
File Type: txt code.txt.txt (5.3 KB, 62 views)
Last edited by JdS : 21-Feb-2004 at 09:50. Reason: Please use a better title in your thread
  #2  
Old 20-Feb-2004, 12:50
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Mitch. I am not 100% sure, why your switch doesn't loop forever. There are no break statements in it. Without a break statement, your code will continue until it hits a break. There should be a break after each one of your case statements.

CPP / C++ / C Code:
switch(choice)
    {
    case 1: enter_matrix(int freemarker, &matrix_a, &matrix_b);
                break;

    case 2: add_matrix(&matrix_a, &matrix_b, &matrix_c);
               break;

    case 5: exit=0;
                break;

    default: break;
    }

I think also, you want to initilize your exit variable to 0 and set it to 1 when on case 5 unless I am missing something.

Also about your free problem. Another solution is to NULL point your pointer when your program begins.

CPP / C++ / C Code:
int main(void)
{

    matrix_struct matrix_a, matrix_b, matrix_c;

    int choice, exit=1, freemarker=0;

    matrix_a->matrix = 0;
    matrix_b->matrix = 0;

That way if free is called it will not do anything.

HTH
d
  #3  
Old 20-Feb-2004, 13:26
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road

Handy.


Quote:
Originally Posted by dsmith
Hi Mitch. I am not 100% sure, why your switch doesn't loop forever. There are no break statements in it. Without a break statement, your code will continue until it hits a break. There should be a break after each one of your case statements.

CPP / C++ / C Code:
switch(choice)
    {
    case 1: enter_matrix(int freemarker, &matrix_a, &matrix_b);
                break;

    case 2: add_matrix(&matrix_a, &matrix_b, &matrix_c);
               break;

    case 5: exit=0;
                break;

    default: break;
    }

I think also, you want to initilize your exit variable to 0 and set it to 1 when on case 5 unless I am missing something.

Also about your free problem. Another solution is to NULL point your pointer when your program begins.

CPP / C++ / C Code:
int main(void)
{

    matrix_struct matrix_a, matrix_b, matrix_c;

    int choice, exit=1, freemarker=0;

    matrix_a->matrix = 0;
    matrix_b->matrix = 0;

That way if free is called it will not do anything.

HTH
d


Thats a trick i never knew. Thanks a lot, that will help bundles. And as for the exiting the menu, i've no idea either. I'll keep on it though. Cheers, and hopefully wont be keeping on bothering you

Toodle
Mitch
  #4  
Old 20-Feb-2004, 14:42
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
First off, indent your do loop and switch
Code:
do { .... freemarker=1; .... printf("Menu:\n\n"); .... printf("\n1 - Enter Matrices A & B"); .... printf("\n2 - Add Matrices"); .... printf("\n3 - Subtract Matrices"); .... printf("\n4 - Multiply Matrices"); .... printf("\n5 - Quit"); .... .... printf("\n\nChoice: "); .... scanf("%d",&choice); .... .... switch(choice) .... { ........ case 1: enter_matrix(int freemarker, &matrix_a, &matrix_b); .... ........ case 2: add_matrix(&matrix_a, &matrix_b, &matrix_c); .... ........ case 5: exit=0; .... ........ default: break; .... } .... }while(exit!=1);
Exit is never set to 1, therefore the loop never exits. Change to:
case 5: exit=1; break;
and add a break; at the end of each case
  #5  
Old 20-Feb-2004, 21:45
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
cheers, though i realised my silly error as soon as i'd actually posted it.

This is giving me so much hassle its unreal.

Silly mistakes like forgetting &'s. and making the exit value the opposite of what it should be. I've been doing this from 4 till half eight, just got home from work and will now be doing it till heaven knows when.

Thanks for all your help, i've got the subtract one sorted, now all i need is the multiply funtion and we're away!

Sleep well,
Mitch
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.