首页 > 解决方案 > 在 C 中将新数据添加到数组中的问题

问题描述

我实际上是编程新手,这是我真正尝试解决的第一件事,但我无法解决。问题是,当我创建一个新帐户时,它没有用第一个帐户昵称更改最后一个帐户的密码。我认为您应该运行代码,创建一个新帐户并按 5 以查看 nicks 数组上的第一个昵称。是因为我还是 Dev C++ 的问题。我不知道。

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

int main()
{
    int right=3,choice1,choice2,choice3,nicksdata=2,passwordsdata=2;
    char nick[20],password[16],user[20];
    char nicks[nicksdata][20]={"emin","ahmet"};
    char passwords[passwordsdata][16]={"1234","1a2s3d"};
    system("cls");
    printf("Hello, welcome.\n");
    entryagain : printf("Press 1 to login your account.\n");
    printf("Press 2 to create an account.\n");
    printf("Press 0 (zero) to shutdown.\n");
    scanf("%d",&choice1);
    switch(choice1)
    {
        case 1 : goto login;
        case 2 : break;
        case 0 : printf("Shutting down."); return 0;
        default : 
        {
            system("cls");
            printf("You have entered an invalid character, please try again.\n\n");
            goto entryagain;
        }
    }
        {
            system("cls");
            nicksdata++;
            passwordsdata++;
            printf("Nickname  : "); scanf("%s",&nicks[nicksdata-1]);
            printf("Password  : "); scanf("%s",&passwords[passwordsdata-1]);
            system("cls");
            printf("Hello,\nWelcome %s\n",nicks[nicksdata-1]);
            goto system;
        }
        
        ///////
        
    login :
        {
            loginagain : system("cls");
            printf("Nickname  : "); scanf("%s",&nick);
            printf("Password  : "); scanf("%s",&password);
            
            for(int i=0;i<nicksdata;i++)
            {
                if(strcmp(nicks[i],nick)==0 && strcmp(passwords[i],password)==0)
                {
                    system("cls");
                    printf("Hello,\nWelcome %s\n",nicks[i]);
                    strcpy(user,nicks[i]);
                    goto system;
                }
                else
                {
                    printf("Nickname or password is wrong. \nPlease try again.(Rights you have : %d)\n",right);
                    right--;
                    if(right==0)
                    {
                        system("cls");
                        printf("You have used all your rights.\n\nShutting down."); return 0;
                    }
                    printf("Press 1 to try again."); scanf("%d",&choice2);
                    switch(choice2)
                    {
                        case 1 : goto loginagain; 
                        case 2 : printf("Shutting down."); return 0;
                        default : printf("You have entered an invalid character, please try again.");
                    }
                }
            }
        }
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    system :
    {
        
        systemagain : printf("\n Computer    - 1");
        printf("\n Games       - 2");
        printf("\n Notes       - 3");
        printf("\n Shutdown    - 4");
        printf("\n\n Choice-----:");
        scanf("%d",&choice3);
        switch(choice3)
        {
            case 1 : goto computer;
            case 2 : goto games;
            case 3 : goto notes;
            case 4 : system("cls"); printf("Shutting down."); return 0;
            case 5 : printf("%s",nicks[0]); // isn't it should be "emin" ???
            return 0;
            default : system("cls"); goto systemagain;
        }
        computer :
            {
                system("cls");
                printf("computer");
                return 0;
            }
        games :
            {
                system("cls");
                printf("games");
                return 0;
            }
        notes :
            {
                system("cls");
                printf("notes");
                return 0;
            }
    }
}

标签: cdev-c++

解决方案


推荐阅读