GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 26-Nov-2008, 04:31
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Help approaching a program


i am wondering if u already studied Class, so u may wanna first combine all ur data into a
CPP / C++ / C Code:
 class student{
// here u set ur private member
string studnt name;
string id;
double gpa;
char grade;
public:
// here goes ur functions;
void checkInput();
/* u can review the ascci table if the entry is not valid use while loop to ask the user to enter the name again*/
student(); // default constructor
student(string, name, string id, double gpa;char grade);
//constructor with parameters;
.... any other functions u wanna put in
};
if it is the case u may wanna say if u studied overloading ostream << >>
  #12  
Old 26-Nov-2008, 09:22
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Help approaching a program


Quote:
Originally Posted by bostero22
Any tips??
Sure. Stop indenting so far. 4 spaces is enough, 3 tabs is way too many. And don't use TABs at all. Set your editor to convert TABs to SPACEs

Quote:
Originally Posted by bostero22
Ok, I could not do it using strings. So now I am trying with chars, maybe thats easier...
Strings can be used just like characters, which means you won't have to make a buffer that "needs to be large, obscenely large". You can still test studentNumber[count].

Quote:
Originally Posted by ocicat
You can check the size of the string entered afterwards (strlen()) to ensure that all of your requirements are met...
Why test all the characters first. Test the size first and save all the trouble of the other tests if the length is wrong. I'd rather check to see if I have enough milk before pouring 10 glasses.


Also, if you are checking a number, using isalnum() will return the wrong information. You want isdigit() to test for numbers. And if you need upper case only, isupper() is the magic function.


Quote:
Originally Posted by zatora
i am wondering if u already studied Class, so u may wanna first combine all ur data into a...
As per the Guidelines (2F specifically), please speak in English. ur, u, and wanna are not words. We are adults here, not children.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #13  
Old 26-Nov-2008, 11:20
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help approaching a program


Quote:
Originally Posted by WaltP
As per the Guidelines (2F specifically), please speak in English. ur, u, and wanna are not words. We are adults here, not children.
More importantly, recognize that the readership of this forum is spread all over the world. Expecting people across the world to be up on hip text messaging lingo in English is expecting too much. English is not the first language of many posters. We should all remain cognizant of using colloquialisms which don't translate. The goal here is to communicate clearly, not be cute.
  #14  
Old 26-Nov-2008, 19:57
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Re: Help approaching a program


I am really sorrry for posting so much, please, take a look at my validation for the student Number (Should be 9 of length and only alpha/digits).. ok the length validation seems to be working right.. but the alpha/digits isn't I also tried using isalnum (which supposely checks for both but still not working).. can you see any errors?

CPP / C++ / C Code:
cout << "Please enter the Student Number: \n";
     getline(cin, studentNumber);
     
     length = studentNumber.length();
     if (length == 9)
     {
                strcpy(studentNum, studentNumber.c_str()); // converts string to character array.
                if (testStudent(studentNum, SIZE)) //boolean function to validate alpha/digit.
                cout << "GREAT" << endl;
                else
                cout << "WRONG" << endl;
                cout << "That is not a valid student Number \n"; 
     }

             
}        
bool testStudent(char number[],int SIZE)
{
     for (int count = 0; count < 10; count++)
     {
         if(!isalpha(number[count]) && isdigit(number[count]))
         return false;
     }
     return true;
           
}
  #15  
Old 26-Nov-2008, 21:54
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help approaching a program


Quote:
Originally Posted by bostero22
can you see any errors?
  • Why are you using strcpy()? Unless you have other requirements which have not been disclosed, it is not necessary.
  • Your indentation style is ambiguous. It is unclear what you think the following code does:
    Quote:
    CPP / C++ / C Code:
                    if (testStudent(studentNum, SIZE)) //boolean function to validate alpha/digit.
                    cout << "GREAT" << endl;
                    else
                    cout << "WRONG" << endl;
                    cout << "That is not a valid student Number \n"; 
    
  • You should spend more time thinking about what the following is doing:
    Quote:
    CPP / C++ / C Code:
            
             if(!isalpha(number[count]) && isdigit(number[count]))
    
    This condition is valid if the character being tested is not alphabetic, but is numeric. In other words, the overall condition is true only for numeric characters.

    Why did you abandon isalnum()?
  #16  
Old 27-Nov-2008, 02:55
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Help approaching a program


Ok, i did my best here i asked you before if you covered topics like Dynamic Array, Classes, Pointers... I hope this will help you to get an idea, you may find a better solution some where else.
This is your Header File:
CPP / C++ / C Code:
#include<string>
using namespace std;
class student
{
string student_number;
string student_name;
// here you will use that for a dynamic array to enter as many classes the user want;
string * student_classes;
int student_credit;
char student_grade;
int student_NumOfClasses;
public:
// constructor with parameter;
student(string number,string name,int credit,char grade,int NumOfClasses );
//constructor with no parameters;
student();
~student();
void print();
};
this is the implementation file.cpp
CPP / C++ / C Code:
#include<iostream>
#include"student.h"
using namespace std;
void student::print()
{
cout<<student_number<<"	"<<student_name<<"	"<<student_credit
<<"	"<<student_grade<<endl;
for(int i=0;i<student_NumOfClasses;i++)
cout<<student_classes[i]<<endl;
}
student::student() 
{
student_number=" ";
student_name=" ";
student_classes=new string;
*student_classes ="No data available";
student_credit=0;
student_grade=' ';
student_NumOfClasses=0;
}
student::student(string number,string name,int credit,char grade,int NumOfClasses)
{
	student_number=number;
	student_name=name;
	student_credit=credit;
	student_grade=grade;
	student_NumOfClasses=NumOfClasses;
	student_classes=new string[NumOfClasses];
	cout<<"Please enter all the classes youv are enrolled in ?"<<endl;
for(int i=0;i<student_NumOfClasses;i++)
{cin>>student_classes[i];}
cout<<endl;
}
student::~student()
{
delete [] student_classes;
}
and this is you client code(the part that involves the int main()
CPP / C++ / C Code:
#include<iostream>
#include"student.h"
using namespace std;
int main()
{ 
// this is a sample print for you to see what is the output
student sample("102-43512","OCICAT",4,'A',3);
sample.print();
cout<<"***************This is how it works in a loop***********"<<endl;
char ch='0';
string number,name;
int credit,numClasses;
char grade;
while(ch !='*')
{
/*please enter only the correct data or the while loop will be infinite*/
cout<<"Enter Student Number ?"<<endl;
cin>>number;
cout<<"Enter Student Name ?"<<endl;
cin>>name;
cout<<"Enter Student Total Credit Hours ?"<<endl;
cin>>credit;
cout<<"Enter Student Grade ?"<<endl;
cin>>grade;
cout<<"Enter Student Number of classes the student is enrolled in ?"<<endl;
cin>>numClasses;
cout<<endl;
student temp(number,name,credit,grade,numClasses);
temp.print();
cout<<"Do you want to continue"<<endl;
cin>>ch;
}
	return 0;
}
if you did cover how to overload the << and >> operators than it is way easier to write it in a file .
Now on the testing the string with 9 digit format you can use this code
CPP / C++ / C Code:
string name,temp;
 cin>>temp;
 if(temp.length()==9)
  {
    for(int i=0;i<9;i++)
    {
      if(temp[i]>=48 ||temp[i]<=57)
      name = temp;
      else
      cout<<"Your entry should include only digit"<<endl;
     }
  }

  else
   cout<<"Your entry should be 9 digit format"<<endl;
by the way this code is to help you the process of verification but you can
modify it according to your needs.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Greatest and smallest integers bostero22 C++ Forum 8 01-Nov-2008 00:13
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.