D
Dawn Minnis
Hi (running Win xp and developing using Miracle C. Running applications in
windows command prompt)
I'm new to the group so be gentle with me. I am currently writing a C
program to perform matrix by matrix (mxm) and matrix by vector (mxv)
multiplication, so obviously one of my first considerations is to ask the
user if they want an mxm or mxv multiplication performed. I have written the
code below (this is a working snippet of the current, substantially larger
program I am working on. My problem at the moment, is that when I run this
code and enter any integer from 0-9 it handles the error accordingly and
iterates around the loop until the values 1 or 2 are entered. Upon which it
prints "successful response" and finishes (again cut down and missing all
the other queries and algorithms).
Now, here comes the problem. When I enter in characters or symbols OTHER
than integers it spirals into a never ending loop, that I can only exit by
closing the command prompt window.
So, can anyone take a quick look at this program and spot for me what I
assume would be a glaringly obvious reason for this to happen? Such as the
use of int for the function declaration, or %d when reading in the value.
But I don't know how to correct the program so that it "accepts" all
possibilities and then handles them.
Thanks, kind regards
Dawn
-*-*-*-program
below-*-*-*-
-----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main()
{
int doMMorMV;
printf("Do you wish to perform MxM or MxV? \nPlease enter 1 for MxM or 2 for
MxV: ");
scanf("%d", &doMMorMV);
while(doMMorMV != 1 && doMMorMV != 2)
{
//call reiterative error message 1
doMMorMV = error1();
}
printf("successful response");
return 0;
}
//reiterative error message 1
int error1()
{
int doMMorMV;
printf("\n Error, not a valid response, \n\t please enter 1 for MxM or 2 for
MxV: \n");
scanf("%d", &doMMorMV);
return doMMorMV;
}
windows command prompt)
I'm new to the group so be gentle with me. I am currently writing a C
program to perform matrix by matrix (mxm) and matrix by vector (mxv)
multiplication, so obviously one of my first considerations is to ask the
user if they want an mxm or mxv multiplication performed. I have written the
code below (this is a working snippet of the current, substantially larger
program I am working on. My problem at the moment, is that when I run this
code and enter any integer from 0-9 it handles the error accordingly and
iterates around the loop until the values 1 or 2 are entered. Upon which it
prints "successful response" and finishes (again cut down and missing all
the other queries and algorithms).
Now, here comes the problem. When I enter in characters or symbols OTHER
than integers it spirals into a never ending loop, that I can only exit by
closing the command prompt window.
So, can anyone take a quick look at this program and spot for me what I
assume would be a glaringly obvious reason for this to happen? Such as the
use of int for the function declaration, or %d when reading in the value.
But I don't know how to correct the program so that it "accepts" all
possibilities and then handles them.
Thanks, kind regards
Dawn
-*-*-*-program
below-*-*-*-
-----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main()
{
int doMMorMV;
printf("Do you wish to perform MxM or MxV? \nPlease enter 1 for MxM or 2 for
MxV: ");
scanf("%d", &doMMorMV);
while(doMMorMV != 1 && doMMorMV != 2)
{
//call reiterative error message 1
doMMorMV = error1();
}
printf("successful response");
return 0;
}
//reiterative error message 1
int error1()
{
int doMMorMV;
printf("\n Error, not a valid response, \n\t please enter 1 for MxM or 2 for
MxV: \n");
scanf("%d", &doMMorMV);
return doMMorMV;
}