-3

I'm having some troubles with this exercise that I'm working on. Basically with this function void check(char *tel, char *c) the first value of the array must be the number 2 and the others 8 must be a number between 0 and 9. If the conditions are met it will print V, otherwise it will print F.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

void check(char *tel, char *c){

int i;

if(*tel!=2) printf("Error\n"); 
                *c='F';
                return;  //I guess this return is wrong

if(*tel==2)
    for(i=0;tel[i]<9;i++){
        if(isdigit(tel[i]));

                   else{
               printf("Error!\n");
               *c='F';
               break;
                               return;}         //this one might be in bad use too

    }
 else

 *c='V';           //having troubles here.. i'm not seeing a way to fix this
 }

 int main(){

 char number[9]={2,3,4,5,6,7,4,5,3};
 char car;


 check(number,&car);

 printf("%c \n", car);

     system("pause");
}
4

2 に答える 2

5

You're missing curly braces around many of your if and else blocks. Properly indenting and formatting your code would go a long way.

if(*tel!=2) printf("Error\n"); *c='F'; return;

For instance, the above should be:

if (*tel != 2) {
    printf("Error\n");
    *c = 'F';
    return;
}

Please, please, please: indent your code properly.


Edit: To be more explicit, here's how your code looks after it's been reformatted. Do you see the errors now, the places where you're missing curly braces?

Hint: I've marked lines that should have them with a /*******/ comment.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

void check (char *tel, char *c) {
    int i;

    if (*tel != 2)                          /*******/
        printf("Error\n"); 

    *c = 'F';
    return;

    if (*tel == 2)                          /*******/
        for (i = 0; tel[i] < 9; i++) {
            if (isdigit(tel[i]))            /*******/
                ;
            else {
                printf("Error!\n");
                *c = 'F';
                break;
                return;
            }
        }
    else                                    /*******/
        *c = 'V';
}

int main() {
    char number[9] = {2, 3, 4, 5, 6, 7, 4, 5, 3};
    char car;

    check(number, &car);

    printf("%c \n", car);

    system("pause");
}
于 2013-03-14T02:22:49.950 に答える
4

There are a lot of small problems with the code you have posted. This is the most immediately obvious to me:

if(*tel!=2) printf("Error\n"); *c='F'; return;  //I guess this return is wrong

Since you have decided not to use curly braces to segment off the if-branch logic, only the first statement - printf("Error\n"); is considered to be associated with your if check. That means that in every execution, the value of c will be set to 'F' and the function will return. The compiler sees this as:

if (*tel != 2) { 
  printf("Error\n");
}
*c = 'F';
return;
// The rest is ignored

You should explicitly use curly braces to mark if branches until you are quite comfortable with the definition of a statement in C++!

于 2013-03-14T02:23:50.320 に答える