C程序 - 输出显示在下一行,不使用换行符

Edd*_*ddy -1 c newline output

在输入所有用户输入后,在此代码中,当被要求代码必须提供相同的输出时.

这是下面给出的代码的一部分,它打印输出但是在打印输出之后city_1[i].city,另一个输出打印在换行符上.

printf发言中,我没用过"\n"之后"%s".

for (i = 0; i <= k - 1; i++) {
    printf("   %s      %d      %f%", city_1[i].city, city_1[i].p, city_1[i].l);
    //printf("\n");
}   
Run Code Online (Sandbox Code Playgroud)

我正在使用Visual Studio 2015来调试我的代码.

任何人都可以在这个问题上帮助解释我吗?

以下是我的代码:

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

//GLOBAL-VARIABLE DECLARTION
#define MAX 1000

//GLOBAL-STRUCTURES DECLARATION
struct census {
    char city[MAX];
    long int p;
    float l;
};

//GLOBAL-STRUCTURE-VARIABLE DECLARATION
struct census cen[MAX] = { 0 };

//USER-DEFINED FUNCTION
struct census sortm_city(struct census city_1[]);
struct census sortm_population(struct census popu[]);
struct census sortm_literacy(struct census lite[]);
void header();

void header() {
    printf("*-*-*-*-*CENSUS_INFO*-*-*-*-*");
    printf("\n\n");
}

//PROGRAM STARTS HERE
main() {    
    //VARIABLE-DECLARATION
    int i = 0, j = 0, n = 0;
    char line[MAX] = { 0 };
    char o = { 0 };

    //FUNCTION CALL-OUT
    header();

    printf("Enter No. of City : ");
    fgets(line, sizeof(line), stdin);
    sscanf_s(line, "%d", &j);

    printf("\n\n");

    printf("Enter Name of City, Population and Literacy level");
    printf("\n\n");

    for (i = 0; i <= j - 1; i++) {
        printf("City No. %d - Info :", i + 1);
        printf("\n\n");

        printf("City Name : ");
        fgets(cen[i].city, MAX, stdin);
        printf("\n");

        printf("Population : ");
        fgets(line, sizeof(line), stdin);
        sscanf_s(line, "%d", &cen[i].p);
        printf("\n");

        printf("Literacy : ");
        fgets(line, sizeof(line), stdin);
        sscanf_s(line, "%f", &cen[i].l);
        printf("\n");

        printf("_____________________________________");
        printf("\n\n");
    }

    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
    printf("Census Information");
    printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
    printf("\n\n");

    for (i = 0; i <= j - 1; i++) {
        printf("City No. %d - Info :", i + 1);
        printf("\n\n");

        printf("City Name : %s", cen[i].city);
        printf("\n");

        printf("Population : %d", cen[i].p);
        printf("\n");

        printf("Literacy : %0.2f", cen[i].l);
        printf("\n");

        printf("_____________________________________");
        printf("\n\n");
    }

    printf("Enter one option from the following : \n\n");
    printf("(a) To Sort the List Alphabetically. \n");
    printf("(b) To Sort the List based on Literacy Level. \n");
    printf("(c) To Sort the List based on Population. \n\n");
    printf("Please type a proper option. \n");

    n = 0;

    while (n == 0) {
        scanf_s(" %c", &o);

        switch (o) {
          case 'a':
            sortm_city(cen, j);
            n = 1;
            break;

          case 'b':
            sortm_population(cen);
            n = 1;
            break;

          case 'c':
            sortm_literacy(cen);
            n = 1;
            break;

          default:
            printf("Option INVALID \n");
            printf("Please type a proper option. \n");
            n = 0;
            break;
        }
    }

    //TERMINAL-PAUSE
    system("pause");
}

struct census sortm_city(struct census city_1[], int k) {
    int i = 0, j = 0;
    //FOR TRANSFERRING
    float b = 0;
    int s = 0;
    char line_0[MAX] = { 0 };

    for (i = 1; i <= k - 1; i++) {
        for (j = 1; j <= k - 1; j++) {
            if (strcmp(city_1[j - 1].city, city_1[j].city) > 0) {
                //SORTING THE LIST ALPHABETICALLY.
                strcpy(line_0, city_1[j - 1].city);
                strcpy(city_1[j - 1].city, city_1[j].city);
                strcpy(city_1[j].city, line_0);

                //COPYING POPULATION AND LITERACY TO RIGHT PLACE.

                //POPULATION :
                s = city_1[j - 1].p;
                city_1[j - 1].p = city_1[j].p;
                city_1[j].p = s;

                //LITERACY : 
                b = city_1[j - 1].l;
                city_1[j - 1].l = city_1[j].l;
                city_1[j].l = b;
            }
        }
    }

    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
    printf("Sorted list in Alphabetical Order");
    printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
    printf("\n\n");

    printf("___________________________________________________\n");
    printf("|   *City Name*   |   Population   |   Literacy   | \n");

    for (i = 0; i <= k - 1; i++) {
        printf("   %s      %d      %f%", city_1[i].city, city_1[i].p, city_1[i].l);
        //printf("\n");
    }
}

struct census sortm_population(struct census popu[]) {
    printf("-------");
}

struct census sortm_literacy(struct census lite[]) {
    printf("-------");
}
Run Code Online (Sandbox Code Playgroud)

Cec*_*rdo 5

fgets包括\n用户在返回的字符串中键入的字符.因此,当您打印该字符串时,您将获得一个新行.