开发者

how to fix my error saying expected expression before 'else'

This program is intended to read a .txt file to get a set of numbers, and write to another two .txt files called even and odd as follows:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
  int i=0,even,odd;
  int number[i];

  // check to make sure that all th开发者_如何转开发e file names are entered
  if (argc != 3) {
     printf("Usage: executable in_file  output_file\n");
      exit(0);
    }

  FILE *dog = fopen(argv[1], "r");
  FILE *feven= fopen(argv[2], "w");
  FILE *fodd= fopen (argv[3], "w");

  // check whether the file has been opened successfully
  if (dog == NULL)
    { printf("File %s cannot open!\n", argv[1]);
      exit(0);
    }

  {  if
    (i%2!=1)
      i++;}

 fprintf(feven, "%d", even);
 fscanf(dog, "%d", &number[i]);
 else {
   i%2==1;
   i++;}
 fprintf(fodd, "%d", odd);
 fscanf(dog, "%d", &number[i]);

fclose(feven);
fclose(fodd);
}


The { appear after the if() condition. And } should come after fscanf(dog, "%d", &number[i]);

if(i%2!=1){
    i++;
    fprintf(feven, "%d", even);
    fscanf(dog, "%d", &number[i]);
}else {
    i%2==1;
    i++;
}


first

int i=0,even,odd;
int number[i];

means the length of Array number is 0. you should write

if (argc != 3) {
    printf("Usage: executable in_file  output_file\n");
    exit(0);
}

FILE *dog = fopen(argv[1], "r");
FILE *feven= fopen(argv[2], "w");
FILE *fodd= fopen (argv[3], "w");
int num;
while (fscanf(dog, "%d", &num) != EOF)
{
    if(num % 2 == 0)
    {
        fprintf(feven, "%d", num);
    }else
    {
        fprintf(fodd, "%d", num);
    }
}
fclose(feven);
fclose(fodd);
fclose(dog);


{ if (i % 2 != 1)
        i++;
}

fprintf(feven, "%d", even);
fscanf(dog, "%d", &number[i]);
else
{
    i % 2 == 1;
    i++;
}

Should be:

if (i % 2 != 1)
{
    i++;
    fprintf(feven, "%d", even);
    fscanf(dog, "%d", &number[i]);
}
else
{
    i % 2 == 1; //BTW this doesn't do anything.
    i++;
}

EDIT: I've taken a few liberties so this might not be exactly your intent but it should be close enough. You need to work on a lot of small things which hopefully looking at the code will help with. Remember to understand what the functions do. And walk before you run.

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


int main(int argc, char *argv[])
{
    int number;

    // check to make sure that all the file names are entered

    if (argc != 4)
    {
        printf("Usage: %s <inputfile> <even_outputfile> <odd_outputfile>\n", argv[0]);
        exit(1);
    }

    FILE *dog   = fopen(argv[1], "r");
    FILE *feven = fopen(argv[2], "w");
    FILE *fodd  = fopen(argv[3], "w");

    // check whether the file has been opened successfully

    if (dog == NULL)
    {
        printf("File %s cannot open!\n", argv[1]);
        exit(1);
    }

    if (feven == NULL)
    {
        printf("File %s cannot open!\n", argv[2]);
        exit(1);
    }

    if (fodd == NULL)
    {
        printf("File %s cannot open!\n", argv[3]);
        exit(1);
    }


    while (fscanf(dog, "%d", &number) == 1)
    {
        if (number % 2 == 0)
            fprintf(feven, "%d ", number);
        else
            fprintf(fodd, "%d ", number);
    }

    fprintf(feven, "\n");
    fprintf(fodd, "\n ");

    fclose(dog);
    fclose(feven);
    fclose(fodd);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜