简体   繁体   English

C 程序没有从我给它的“正确”输入中给出所需的输出

[英]C program is not giving out the required output from the "correct" input I'm giving it

I am reposting this because my previous post was closed down due to improper formatting;我重新发布这个是因为我之前的帖子由于格式不正确而被关闭; hopefully that's fixed.希望这是固定的。

When I give it the correct input which is an IPv4 with the submask of 8, 16, 24, or 32, it does nothing and still takes in input when I press enter, whilst it obviously should return an output of the broadcasting IP's.当我给它正确的输入(子掩码为 8、16、24 或 32 的 IPv4)时,它什么也不做,但在我按下 Enter 键时仍然接受输入,而它显然应该返回广播 IP 的输出。 What am I doing wrong in the following code that I'm getting this error?我在下面的代码中做错了什么,我收到了这个错误? I have tried debuggers but none of them are helping me solve the issue so far.我试过调试器,但到目前为止,它们都没有帮助我解决问题。 This is for an assignment, and the usage of Arrays is not allowed.这是一个赋值,不允许使用数组。

#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;
}

There are several problems in your code!你的代码有几个问题! The first (and most serious) error is that you are passing values by value - which will mean the functions receive "copies" of the data, and cannot change the values in the calling code.第一个(也是最严重的)错误是您按值传递值 - 这意味着函数接收数据的“副本”,并且无法更改调用代码中的值。 To fix this, you need to declare the arguments as pointers and pass the addresses of the variables to change.要解决此问题,您需要将参数声明为指针并传递要更改的变量地址

Another problem that you have (in a few places) is that you are assigning variables the return value of the scanf function: this is wrong , as that will be the count of how many values were (successfully) read by scanf .您遇到的另一个问题(在一些地方)是您将scanf函数的返回值分配给变量:这是错误的,因为这将是scanf (成功)读取了多少值的计数

Here's a 'fixed' version of your code, with triple-slash comments (///) added with notes where I've made changes:这是您的代码的“固定”版本,添加了三斜线注释 (///) 以及我所做更改的注释:

#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;
}

Feel free to ask for further clarification and/or explanation.随时要求进一步澄清和/或解释。

Your code has some problems:您的代码有一些问题:

  • You are passing integers by parameters.您正在通过参数传递整数。 Those values are getting copied to the called function, when you modify them in the function, you are modifying only the version that lives inside the function.这些价值越来越被复制到被调用的函数,当你在函数修改它们,只修改的版本,该函数内的生活 Check Pass By Value检查传递值

  • As a consequence going to the scanf taking the pointer of that parameter value is not recommended in C, as you are taking the pointer of the copy.因此,在 C 中不建议使用该参数值的指针转到scanf ,因为您正在获取副本的指针。

  • Review the return of the second scanf I think is not your intention复查第二次扫描的返回我认为不是你的本意

  • Review the return value of your functions, you are not actually using the value.查看函数的返回值,您实际上并未使用该值。

My recommendation, (apart from from proper code structure) for fixing this code would be using references for your int values.我的建议(除了正确的代码结构)修复此代码将使用引用作为您的 int 值。

But first take a read on how Pointers in C work .但首先阅读C 中的指针是如何工作的 Also ensure to understand the * & operators.还要确保理解 * & 运算符。

  • & symbol is used to get the address of the variable. &符号用于获取变量的地址。

  • * symbol is used to get the value of the variable that the pointer is pointing to. * 符号用于获取指针指向的变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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