String Error validation thru a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Theadmin77
    New Member
    • Nov 2006
    • 19

    String Error validation thru a function

    I dont know what i am doing wrong , i am trying to validate a string entry (first name to only take lower and upper case letters) thru a function , but for some reason is not working ....=0(

    Any idea why ?

    This is the code

    #include<iostre am>

    using namespace std;
    bool getstring (char[30]);

    struct nameuser
    {
    char first[30];
    char last [30];
    };

    struct studentype
    {
    nameuser name;
    char nid [30];
    float score;
    };

    void main()
    {
    studentype student [100];
    float curve=0.0;
    int i;
    int nstudents;
    bool validata;

    do
    {
    cout<<"Enter the number of students [0-100]:";
    cin>>nstudents;

    if ((nstudents<1)| |(nstudents>100 ))
    {
    cout<<"Incorrec t value .Try Again !!"<<endl;
    }
    }while ((nstudents<1)| |(nstudents>100 ));


    for (i=0;i<nstudent s;i++)
    {
    cout <<"Enter information for student#"<<i+1< <endl;

    do
    {
    cout <<"First name :";

    validata= getstring(stude nt[i].name.first);

    if(!validata)
    {
    cout <<"Wrong input ...Try Again !!! \n";
    }

    }while (!validata);


    cout <<"Last name :";
    cin >>student[i].name.last;
    cout <<"nid:";
    cin >>student[i].nid;
    cout <<"Enter score between [0-100]:";
    cin >>student[i].score;

    }
    };

    // function to check valid data:


    bool getstring (char[]);

    {

    int i;
    bool noerrors = true;

    cin.getline (student[i].name.first,30) ;

    i=0;

    while ((i< strlen(student[i].name.first)) && noerrors)

    {
    if ((student[i].name.first<'A' ) || (student[i].name.first[i]>'z'))
    {
    lenght=strlen(s tudent[i].name.first);

    noerrors=false;
    }

    i++;
    }

    return noerrors;
    }
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    I did some changes in your code to make it work.

    Code:
    #include<iostream>
    
    using namespace std;
    
    struct structUserName
    {
       char szFirst[30];
       char szLast[30];
    };
    
    struct structStudent
    {
       structUserName name;
       char  nID[30];
       float fScore;
    };
    
    bool GetString (char *a_pszString)
    {
       int 
          i = 0,
          nStrLength = 0;
       
    //   cin.getline (a_s1.name.szFirst,30);
    //   fgets(a_s1.name.szFirst, sizeof(a_s1.name.szFirst), stdin);
       cin >> a_pszString; 
    
       nStrLength = strlen(a_pszString);
       for(i = 0; i < nStrLength; i++)
       {
          if(isalpha(a_pszString[i]) == false)
          {
             return false;
          }      
       }   
       return true;
    }
    
    
    void main()
    {
       structStudent
          student [100];
       float 
          fCurve = 0.0;
       int 
          i = 0,
          nStudents = 0;
    
       bool 
          bValidData = false;
    
       do
       {
          cout<<"Enter the number of students [0-100]:";
          cin>>nStudents;
    
          if (nStudents<1 || nStudents>100)
          {
             cout<<"Incorrect value .Try Again !!"<<endl;
          }
       }
       while (nStudents<1 || nStudents>100);
    
    
       for (i=0; i < nStudents; i++)
       {
          cout <<"Enter information for student#"<<i+1<<endl;
    
          do
          {
             cout <<"First name :";
    
             bValidData = GetString(student[i].name.szFirst);
             if(bValidData == false)
             {
                cout <<"Wrong input ...Try Again !!! \n";
             }
          }
          while (bValidData == false);
    
          cout <<"Last name :";
          cin >>student[i].name.szLast;
          cout <<"nID:";
          cin >>student[i].nID;
          cout <<"Enter Score between [0-100]:";
          cin >>student[i].fScore;
       }
    };
    Regards,
    M.Sivadhas.

    Comment

    • Theadmin77
      New Member
      • Nov 2006
      • 19

      #3
      Thank you very much for your help!!!!

      Comment

      • sivadhas2006
        New Member
        • Nov 2006
        • 142

        #4
        Originally posted by Theadmin77
        Thank you very much for your help!!!!
        Hi,

        You are welcome to http://www.thescripts.com/.

        Regards.
        M.Sivadhas.

        Comment

        Working...