我之所以重新发布,是因为我的上一则帖子由于格式不正确而被关闭;希望这是固定的。

当我给它正确的输入,即一个带有8、16、24或32子掩码的IPv4时,它什么也不做,当我按Enter键时仍然接受输入,而它显然应该返回广播IP的输出。在以下代码中出现此错误,我在做什么错?我已经尝试过调试器,但是到目前为止,它们都没有帮助我解决问题。这是用于分配的,并且不允许使用数组。

#include <stdio.h>

int IPAndSubnetMaskInput(int IPAndSubnetMaskFirstOctate, int IPAndSubnetMaskSecondOctate, int IPAndSubnetMaskThirdOctate, int IPAndSubnetMaskFourthOctate, int IPAndSubnetMaskSubnetInput) {

    printf("Please input an IPv4 address followed by the subnetmask (must either be 8, 16, 24, or 32) in the following format: 192 168 1 1 32: \n"); //Statement to ask for IP and submask input and puts the inputs into a variable to calculate Broadcast IP

    int IPv4AndSubmask = scanf("%d %d %d %d %d", &IPAndSubnetMaskFirstOctate, &IPAndSubnetMaskSecondOctate, &IPAndSubnetMaskThirdOctate, &IPAndSubnetMaskFourthOctate, &IPAndSubnetMaskSubnetInput); //Input function

    while (!(IPAndSubnetMaskSubnetInput == 8 || IPAndSubnetMaskSubnetInput == 16 || IPAndSubnetMaskSubnetInput == 24 || IPAndSubnetMaskSubnetInput == 32)) { //Initializing loop to evaluate whether subnet is correct or not

        printf("Your submask is wrong. Please enter a value that's either 8, 16, 24, or 32: \n");
        IPAndSubnetMaskSubnetInput = scanf("%d", &IPAndSubnetMaskSubnetInput);

        if (IPAndSubnetMaskSubnetInput == 8 || IPAndSubnetMaskSubnetInput == 16 || IPAndSubnetMaskSubnetInput == 24 || IPAndSubnetMaskSubnetInput == 32)
        {
            break;
        }
    }

    return IPv4AndSubmask;//function returns the value of IP octates and the subnet mask for the program to calculate
}

int broadcastCalculator(int broadcastFirstOctate, int broadcastSecondOctate, int broadcastThirdOctate, int broadcastFourthOctate, int broadcastSubnetInput) { //Declaration of first function for first Assignment point
    IPAndSubnetMaskInput(broadcastFirstOctate, broadcastSecondOctate, broadcastThirdOctate, broadcastFourthOctate, broadcastSubnetInput);

    while (0 == 0) {

        if (broadcastSubnetInput == 8) { //Conditional statement for submask of 8
            printf("The broadcast IP is:\t%hhu\t%hhu\t%hhu\t255\t\n", broadcastFirstOctate, broadcastSecondOctate, broadcastThirdOctate);//Program will print the Broadcast IP of firstOctate  secondOctate thirdOctate 255
            break;
        }
        else if (broadcastSubnetInput == 16) {//Conditional statement for submask of 16
            printf("The broadcast IP is:\t%hhu\t%hhu\t255\t255\t\n", broadcastFirstOctate, broadcastSecondOctate);//Program will print the Broadcast IP of firstOctate  secondOctate 255 255
            break;
        }
        else if (broadcastSubnetInput == 24) {//Conditional statement for submask of 24
            printf("The broadcast IP is:\t%hhu\t255\t255\t255\t\n", broadcastFirstOctate);//Program will print the Broadcast IP of firstOctate  255 255 255
            break;
        }
        else if (broadcastSubnetInput == 32) {//Conditional statement for submask of 32
            printf("The broadcast IP is:\t255\t255\t255\t255");//Program will print the Broadcast IP of 255  255 255 255
            break;
        }
    }
    return 0;
}


int main()

{
    int FARfirstOctate = 0; int FARsecondOctate = 0; int FARthirdOctate = 0; int FARfourthOctate = 0; int FARsubnetInput = 0;
    broadcastCalculator(FARfirstOctate, FARsecondOctate, FARthirdOctate, FARfourthOctate, FARsubnetInput);

    return 0;
}

最佳答案

您的代码中有几个问题!第一个(也是最严重的)错误是您正在按值传递值-这意味着函数将接收数据的“副本”,并且无法更改调用代码中的值。要解决此问题,您需要将参数声明为指针,并传递变量的地址以进行更改。

您(在少数地方)遇到的另一个问题是,您正在为变量分配scanf函数的返回值:这是错误的,因为这将是scanf读取(成功)了多少个值的计数。

这是代码的“固定”版本,在我进行了更改的地方加上了三斜杠注释(///),并加上了注释:

#include <stdio.h>

/// Declare the arguments as POINTERS - so we can change the values in the calling code ...
int IPAndSubnetMaskInput(int* IPAndSubnetMaskFirstOctate,
                         int* IPAndSubnetMaskSecondOctate,
                         int* IPAndSubnetMaskThirdOctate,
                         int* IPAndSubnetMaskFourthOctate,
                         int* IPAndSubnetMaskSubnetInput)
{
    printf("Please input an IPv4 address followed by the subnetmask (must either be 8, 16, 24, or 32) in the following format: 192 168 1 1 32: \n"); //Statement to ask for IP and submask input and puts the inputs into a variable to calculate Broadcast IP

    /// This line WAS WRONG - the return value from scanf is the COUNT of values read!
    int IPv4AndSubmask = 0; /// We need to do something here to get a meaningful return value
    scanf("%d %d %d %d %d", /// As they are ALREADY pointers now, we don't need the addresses ...
        IPAndSubnetMaskFirstOctate,
        IPAndSubnetMaskSecondOctate,
        IPAndSubnetMaskThirdOctate,
        IPAndSubnetMaskFourthOctate,
        IPAndSubnetMaskSubnetInput); //Input function

    while (!(*IPAndSubnetMaskSubnetInput == 8 ||  /// As they're now all POINTERS, we need to dereference them...
             *IPAndSubnetMaskSubnetInput == 16 ||
             *IPAndSubnetMaskSubnetInput == 24 ||
             *IPAndSubnetMaskSubnetInput == 32))
    { //Initializing loop to evaluate whether subnet is correct or not

        printf("Your submask is wrong. Please enter a value that's either 8, 16, 24, or 32: \n");
        /// This line is WRONG - the return value from scanff is the COUNT of values read!
    //  IPAndSubnetMaskSubnetInput = scanf("%d", IPAndSubnetMaskSubnetInput);
        scanf("%d", IPAndSubnetMaskSubnetInput); /// Already a pointer!

     /// We don't need this check - the loop will exit when the condition is matched!
     //   if (*IPAndSubnetMaskSubnetInput == 8 || /// As they're now all POINTERS, we need to dereference them...
     //       *IPAndSubnetMaskSubnetInput == 16 ||
     //       *IPAndSubnetMaskSubnetInput == 24 ||
     //       *IPAndSubnetMaskSubnetInput == 32)
     //   {
     //       break;
     //   }
    }
    return IPv4AndSubmask;//function returns the value of IP octates and the subnet mask for the program to calculate
}

/// Declare theargumetns as POINTERS - so we can change the values in the calling code ...
int broadcastCalculator(int* broadcastFirstOctate,
                        int* broadcastSecondOctate,
                        int* broadcastThirdOctate,
                        int* broadcastFourthOctate,
                        int* broadcastSubnetInput)
{ //Declaration of first function for first Assignment point
    IPAndSubnetMaskInput(broadcastFirstOctate,
                         broadcastSecondOctate,
                         broadcastThirdOctate,
                         broadcastFourthOctate,
                         broadcastSubnetInput);
    while (0 == 0) {
        /// As before, we now need to DEREFENCE the pointers...
        if (*broadcastSubnetInput == 8) { //Conditional statement for submask of 8
            printf("The broadcast IP is:\t%hhu\t%hhu\t%hhu\t255\t\n",
                *broadcastFirstOctate,
                *broadcastSecondOctate,
                *broadcastThirdOctate);
            //Program will print the Broadcast IP of firstOctate  secondOctate thirdOctate 255
            break;
        }
        else if (*broadcastSubnetInput == 16) {//Conditional statement for submask of 16
            printf("The broadcast IP is:\t%hhu\t%hhu\t255\t255\t\n",
                *broadcastFirstOctate,
                *broadcastSecondOctate);
            //Program will print the Broadcast IP of firstOctate  secondOctate 255 255
            break;
        }
        else if (*broadcastSubnetInput == 24) {//Conditional statement for submask of 24
            printf("The broadcast IP is:\t%hhu\t255\t255\t255\t\n",
                *broadcastFirstOctate);
            //Program will print the Broadcast IP of firstOctate  255 255 255
            break;
        }
        else if (*broadcastSubnetInput == 32) {//Conditional statement for submask of 32
            printf("The broadcast IP is:\t255\t255\t255\t255");
           //Program will print the Broadcast IP of 255  255 255 255
           break;
        }
    }
    return 0;
}

int main()
{
    int FARfirstOctate = 0; int FARsecondOctate = 0; int FARthirdOctate = 0; int FARfourthOctate = 0; int FARsubnetInput = 0;
    broadcastCalculator(&FARfirstOctate, &FARsecondOctate, &FARthirdOctate, &FARfourthOctate, &FARsubnetInput);
    return 0;
}


随时要求进一步的澄清和/或解释。

关于c - C程序没有从“正确的”输入中给出所需的输出,而是在给出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59111022/

10-15 15:45