scanf函数验证 [英] Scanf Validation

查看:120
本文介绍了scanf函数验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我确认的输入,scanf函数的,我有以下。我希望程序要求对数据进行重新输入,如果scanf函数的是一个不正确的范围内整数与否。我把一个do while循环之前,if语句,但是当我编译它第一个printf和scanf只是循环

Can someone help me validate the inputs for the Scanf's i have below. I want the programme to ask for the data to be re-entered if the Scanf's are within an incorrect range or not an interger. I put in a do while loop before with an if statement but when i compiled it the first printf and scanf just looped

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



int MenuLoop = 0;
int MaxPackets = 4;
int currentPackets= 0; 
int menu;



/*********************************************************
* Node to represent a Cat which includes a link reference*
* a link list of nodes with a pointer to a Cat Struct    *
* would be better but this is for illustartion only!     *
**********************************************************/
struct Packet {
int Source;
int Destination;
int Type;
int Port;
char *Data;
struct Packet *next; // Link to next Cat
};

typedef struct Packet node; // Removes the need to constantly refer to struct


/*********************************************************
* Stubs to fully declared functions below                *
**********************************************************/
void outputPackets(node **head);
void push(node **head, node **aPacket);
node* pop(node **head);
void AddPacket();
void AddPacket();
void SavePacket();
void ShowCurrent();
void ExitProgramme();



main() {

do{

Menu();

} while(menu<4);


}


void AddPacket(){

int option;





/*********************************************************
* pointers for the link list and the temporary P to    *
* insert into the list                                   *
**********************************************************/
node *pPacket, *pHead = NULL;

/*********************************************************
* Create a cat and also check the HEAP had room for it   *
**********************************************************/
pPacket = (node *)malloc(sizeof(node));
if (pPacket == NULL)
{
    printf("Error: Out of Memory\n");
    exit(1);
}

currentPackets++;
printf("Enter Source Number between 1-1024:\n");
scanf("%i", &pPacket->Source);
printf("Enter Destination Number between 1-1024:\n");
scanf("%i", &pPacket->Destination);
printf("Enter Type Number between 0-10:\n");
scanf("%i", &pPacket->Type);
printf("Enter Port Number between 1-1024:\n");
scanf("%i", &pPacket->Port);
printf("Enter Data Numberbetween 1-50:\n");
scanf("%s", &pPacket->Data);
printf("Do you want to Enter another Packet?");
pPacket->next = NULL;

/*********************************************************
* Push the Cat onto the selected Link List, the function *
* is written so the program will support multiple link   *
* list if additional 'pHead' pointers are created.       *
* Who says you cannot herd cats!                         *
**********************************************************
* NOTE: The push parameters are using references to the  *
* pointers to get round the pass by value problem caused *
* by the way C handles parameters that need to be        *
* modified                                               *
**********************************************************/

push(&pHead, &pPacket);

pPacket = (node *)malloc(sizeof(node));
if (pPacket == NULL)
{
    printf("Error: Out of Memory\n");
    exit(1);
}

 outputPackets(&pHead);

/*********************************************************
* Display the Link List 'pHead' is passed as a reference *
**********************************************************/


return 0;


do{
    if(currentPackets == MaxPackets);
{
    printf("Packet limit reached please save\n");

}


}while(currentPackets<MaxPackets);

return 0;
}




void outputPackets(node **head)
{


/*********************************************************
* Copy Node pointer so as not to overwrite the pHead     *
* pointer                                                *
**********************************************************/
node *pos = *head;

/*********************************************************
* Walk the list by following the next pointer            *
**********************************************************/
 while(pos != NULL) {
    printf("Source: %.4i Destination: %.4i Type: %.4i Port: %.4i \n", pos->Source,   pos->Destination, pos->Type, pos->Port);

    pos = pos->next ;
}
printf("End of List\n\n");
}



void push(node **head, node **aPacket)
{
/*********************************************************
* Add the cat to the head of the list (*aCat) allows the *
* dereferencing of the pointer to a pointer              *
**********************************************************/
(*aPacket)->next = *head;
*head = *aPacket;
}

node *pop(node **head)
{
/*********************************************************
* Walk the link list to the last item keeping track of   *
* the previous. when you get to the end move the end     *
* and spit out the last Cat in the list                  *
**********************************************************/
node *curr = *head;
node *pos = NULL;
if (curr == NULL)
{
    return NULL;
} else {
    while (curr->next != NULL)
    {
        pos = curr;
        curr = curr->next;
    }
    if (pos != NULL) // If there are more cats move the reference
    {
        pos->next = NULL;
    } else {         // No Cats left then set the header to NULL (Empty list)
        *head = NULL;
    }
}
 return curr;

}




void SavePacket(){



FILE *inFile ;
char inFileName[10] = { '\0' } ;

printf("Input file name : ") ;
scanf("%s", inFileName) ;

//Open file
inFile = fopen(inFileName, "w+");
if (!inFile)
{
fprintf(stderr, "Unable to open file %s", &inFile);
exit(0);

}

//fprintf(inFile, "Source: %i Destination: %i Type: %i Port: %i \n", pos->Source,     pos->Destination, pos->Type, pos->Port);
fclose(inFile);

}







void ShowCurrent(){



}

void ExitProgramme(){}

void Menu(){


printf("********Welcome****** \n");
printf("Creator Ben Armstrong.\n\n");
printf("*Please Choose an option*\n");
printf("1. Add a new packet\n");
printf("2. Save current packet to file\n");
printf("3. Show current list of packets\n");
printf("4. Exit\n");

scanf("%i", &menu);

    switch(menu)

    {
    case 1:
    AddPacket();
    break;

    case 2:
        SavePacket();
    break;

    case 3 :
        ShowCurrent();
    break;

    case 4 :
    ExitProgramme();
    break;



}


}

这是我的全code,如u可以看到我尝试执行该数据已被确认为一个链接列表

This is my full code as u can see im trying to implement a link list which the data has to be validated for

推荐答案

下面是一个使用一种方法与strtol

Here is one approach using strtol:

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

struct Packet {
    int Source;
    int Destination;
    int Type;
    int Port;
    int Data;
};

void func(struct Packet *pPacket) {
    char entry[100];
    int i;
    char *tail;
    do {
        printf("Enter Source Number between 1-1024:\n");
        scanf("%99s", entry);
        i = strtol(entry, &tail, 0);

    } while (*tail || i < 1 || i > 1024);
    pPacket->Source = i;
}

int main(int argc, char* argv[])
{
    struct Packet pPacket;

    func(&pPacket);

    printf("pPacket->Source is now %i.\n", pPacket.Source);

    return 0;
}

这篇关于scanf函数验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆