简体   繁体   中英

How do I fix this for loop problem in C++?

I have this for loop problem in my c++ program. When i try to run it it's all working fine but there is a small problem in the "Enter name:" loop. The "Enter name:" outputs 5 times but the first "Enter name:" is not recognized as part of the loop but instead the 6th empty line before the last "Enter name:" loop is recognized as one but it does not have a "Enter name:" in it, it is just an empty line with no words but the program recognized it as part of the loop not the one in the very first line with the "Enter name:". Can someone help me to fix and recognize the first line with the "Enter name:" as part of the loop and not recognize the 6th line of the program as part of the loop.

这是程序输出

This is my code for my c++ program:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

int main()
{
    char names[5][20],t[20];
    int i,j;
    for ( int i = 0; i < 5; i++ )
    {
        cout << "Enter name: ";
        cin.getline( names[i], 20 );
    }
        {
                cout<<" ";
                cin>>names[i];
        }
        for(i=1; i<5; i++)
        {
                for(j=1; j<5; j++)
                {
                        if(strcmp(names[j-1], names[j])>0)
                        {
                                strcpy(t, names[j-1]);
                                strcpy(names[j-1], names[j]);
                                strcpy(names[j], t);
                        }
                }
        }
        cout<<"\n Names Sorted in Alphabetical Order : \n\n";
        for(i=0; i<5; i++)
        {
                cout<<" ";
                cout<<names[i]<<"\n";
        }
        return 0;
}

There are 2 problems with your code:

Mistake 1

The variable i is uninitialized and you're using that uninitialized variable when you wrote:

cin>>names[i]; //this is undefined behavior since `i` is uninitialized

which leads to undefined behavior .

Undefined behavior means anything 1 can happen including but not limited to the program giving your expected output. But never rely (or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.

Mistake 2

You have an unnecessary block(given below) in your code:

{
    cout<<" ";
    cin>>names[i];
}

The above block does not belong to any for loops and is completely unnecessary. You should remove the above shown block.

Additionally, you need to take care of the fact that you must not go out of bounds of any of the array in your program. For example, say if you have an array named arr of length 5 then you can safely access elements: arr[0] , arr[1] , arr[2] , arr[3] and arr[4] . But if we try to access arr[5] then it will lead to undefined behavior.


1 For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM