【问题标题】:Writing to file and using if statments with loops写入文件并使用带有循环的 if 语句
【发布时间】:2018-04-27 11:25:35
【问题描述】:

我正在创建一个简单的系统,供人们输入基本详细信息,将它们打印在屏幕上,一旦确认写入文本文件,用户输入的信息是否错误?报告,如果输入了另一个输入,它会再次询问问题。我正在努力让打印文件工作和两个结束循环。

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

int get_line(const char *prompt, char *dest, size_t size) {
  printf("%s", prompt);
  fflush(stdout);
  if (fgets(dest, size, stdin) == NULL) {
    dest[0] = '\0';
    return 0;
  }
  dest[strcspn(dest, "\n")] = '\0';  // Lop off potential trailing '\n'
  return 1;
}

int main(void) 
{
    char first_name[20], surname[20], street_no[10], street_name[40], postcode[10], contact_no[20], save_edit_qu[10];
    int dd, mm, yy;
    get_line(" Enter first name:\n", first_name, sizeof first_name);
    get_line(" Enter surname:\n", surname, sizeof surname);
    get_line(" Contact Number\n", contact_no, sizeof contact_no);
    get_line(" Street Number\n", street_no, sizeof street_no);
    get_line(" Street Name\n", street_name, sizeof street_name);
    get_line(" Postcode\n", postcode, sizeof postcode);

    printf(" First Name : %s\n", first_name);
    printf(" Surname    : %s\n", surname);
    printf(" Contact No.: %s\n", contact_no);
    printf(" Street No. : %s\n", street_no);
    printf(" Stret Name : %s\n", street_name);
    printf(" Postcode   : %s\n", postcode);


    get_line(" If the informations above is correct please enter SAVE/if you wish to change any informations please enter edit", save_edit_qu, sizeof save_edit_qu);
    if (save_edit_qu[0] == 'SAVE' || save_edit_qu[0] == 'save') {
    //write info to file
    }
    if (save_edit_qu[0] == 'EDIT' || save_edit_qu[0] == 'edit') {
    //loop back to beginning of report
    }
    else if ()//loop to beginning of SAVE/EDIT QU

    return 0;
}

【问题讨论】:

  • else if ()//loop to start of SAVE/EDIT QU 你的无限循环在哪里?类似于 while(1){...}

标签: c file loops if-statement


【解决方案1】:

使用 strcmp() 并使用双引号 "" 表示字符串!

if (strcmp(save_edit_qu,"SAVE") == 0 || strcmp(save_edit_qu,"save") == 0) {

或者像这样使用单引号只测试第一个字符

if (save_edit_qu[0] == 'S' || save_edit_qu[0] == 's') {

【讨论】:

    【解决方案2】:

    因此,您的程序存在一些问题。在向您展示更改之前,我将尝试在此处介绍所有内容。

    1. 您的字符串比较很简单:save_edit_qu[0] == 'SAVE' 只是将save_edit_qu 的第一个字符/字节与'SAVE' 进行比较,这本身不是正确的字符串文字。您必须在 C 中用双引号将字符串文字括起来。即使您在这里这样做了,将字符与字符串进行比较也没有任何意义。您应该做的是使用string.h 中的strcmp 为您进行比较。我已经把它放在我的固定版本的程序中。格式为:strcmp(a,b) == 0 如果字符串a 等于字符串b

    2. 您让用户能够编辑他们的所有输入数据。因此,您应该将数据收集放在一个循环中。这使您可以在用户未完成时不断地重新收集数据。

      do { // collect data. } while (!done);

    3. 最后,您要让用户在数据收集循环中执行一项操作,以便他们可以选择他们想要对数据执行的操作。他们可以编辑或保存。还有一种情况是他们都没有进入。在这种情况下,将再次提示他们。这保证了内部的另一个控制循环。

      do {
          // collect data
          do {
              // save or edit
          } while (!validChoice);
      
      } while (!done);
      

    话虽如此,这是工作程序。不过,我还没有为您实现写入文件部分。我想你可以自己试一试!

    #include <stdio.h>
    #include <string.h>
    
    int get_line(const char *prompt, char *dest, size_t size) {
      printf("%s", prompt);
      fflush(stdout);
      if (fgets(dest, size, stdin) == NULL) {
        dest[0] = '\0';
        return 0;
      }
      dest[strcspn(dest, "\n")] = '\0';  // Lop off potential trailing '\n'
      return 1;
    }
    
    int main(void) 
    {
        char first_name[20], surname[20], street_no[10], street_name[40], postcode[10], contact_no[20], save_edit_qu[10];
        int dd, mm, yy, done = 0;
    
        // Data collection loop: Runs as long as the user opts to edit the data.
        do {
    
            // Fetch data.
            get_line(" Enter first name:\n", first_name, sizeof first_name);
            get_line(" Enter surname:\n", surname, sizeof surname);
            get_line(" Contact Number\n", contact_no, sizeof contact_no);
            get_line(" Street Number\n", street_no, sizeof street_no);
            get_line(" Street Name\n", street_name, sizeof street_name);
            get_line(" Postcode\n", postcode, sizeof postcode);
    
            printf(" First Name : %s\n", first_name);
            printf(" Surname    : %s\n", surname);
            printf(" Contact No.: %s\n", contact_no);
            printf(" Street No. : %s\n", street_no);
            printf(" Stret Name : %s\n", street_name);
            printf(" Postcode   : %s\n", postcode);
    
            // Action loop: Runs as long as no valid input is given.
            do {
                get_line(" If the informations above is correct please enter SAVE/if you wish to change any informations please enter edit\n", save_edit_qu, sizeof save_edit_qu);
    
                // Option to quit.
                if (strcmp(save_edit_qu, "SAVE") == 0 || strcmp(save_edit_qu, "save") == 0) {
                    fprintf(stdout, "Writing data to file...\n");
                    // write data here.
    
                    // Set done flag, and exit action loop.
                    done = 1;
                    break;
                }
    
                // Option to edit.
                if (strcmp(save_edit_qu, "EDIT") == 0 || strcmp(save_edit_qu, "edit") == 0 ) {
    
                    //loop back to beginning of report
                    break;
                }
    
                // Otherwise ask prompt again ^.
            } while (1);
    
        } while (!done);
    
    
        return 0;
    }
    

    【讨论】:

    • 谢谢 Micrified,这个很有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多