By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

I'm having trouble. Help?

P: n/a
I can't get this code to work right. It seems to be skipping some of the cin
functions. Can someone help me with this?

ClassTrack.cpp:

#include <iostream>
#include "ClassTrack.h"
using namespace std;

const MAX_CLASSES = 12;
ClassType classes[MAX_CLASSES];
int classIndex;

int main(){
classIndex = 0;
bool quit = false;
int choice;

do {
cout << "Welcome to Class Tracker. What would you like to do? \n";
cout << "\t(1) Enter new Class Data \n";
cout << "\t(2) Print Class Data \n";
cout << "\t(3) Quit \n";

cin >> choice;

switch(choice){
case 1: EnterData();
break;
case 2: PrintData();
break;
case 3: quit = true;
break;
default: cout << "\nThat is an invalid choice. Please try
again\n";
system("cls");
break;
}
}while(quit == false);

return 0;
}

void EnterData(){
int choice;
system("cls");

cout << "How many classes do you wish to enter?\n";
cin >> choice;

for(int i = 0; i < choice; i++){
cout << "\n Name of class:";
cin.getline(classes[classIndex].nm, 25);
cout << "\n Class ID:";
cin >> classes[classIndex].id;
cout << "\n Class meets on:";
cin.getline(classes[classIndex].meets, 7);
cout << "\n Starting Time:";
cin.getline(classes[classIndex].start, 10);
cout << "\n Ending Time:";
cin.getline(classes[classIndex].end, 10);
cout << "\n Teacher:";
cin.getline(classes[classIndex].teacher, 25);
cout << "\n Number of students:";
cin >> classes[classIndex].numStudents;
classIndex++;
}
}

void PrintData(){
char choice[4];
system("cls");

cout << "Type the class ID of the class that you want\n";
cout << "to view, or type 'all' to print all of them.\n";
cin.getline(choice, 3);

if(choice == "all"){
for(int i = 0; i < classIndex; i++){
cout << classes[i].nm << endl;
cout << classes[i].id << endl;
cout << classes[i].meets << endl;
}
//Need to implement individual printing
}

ClassTrack.h:

void EnterData();
void PrintData();

struct ClassType {
char nm[25];
int id;
char meets[7];
char start[10];
char end[10];
char teacher[25];
int numStudents;
};

Please help.
Dec 26 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Jordan Tiona wrote:
I can't get this code to work right. It seems to be skipping some of the cin
functions. Can someone help me with this?


http://www.parashift.com/c++-faq-lit...t.html#faq-5.7
http://www.parashift.com/c++-faq-lit....html#faq-15.6
http://www.parashift.com/c++-faq-lite
Jonathan

Dec 26 '05 #2

P: n/a
Sorry, and thank you. One last thing. Is one not allowed to have symbols in
a char array? For class name, I typed C++, and it ended the EnterData
function and did an infinite loop in main.

"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Jordan Tiona wrote:
I can't get this code to work right. It seems to be skipping some of the
cin
functions. Can someone help me with this?


http://www.parashift.com/c++-faq-lit...t.html#faq-5.7
http://www.parashift.com/c++-faq-lit....html#faq-15.6
http://www.parashift.com/c++-faq-lite
Jonathan

Dec 26 '05 #3

P: n/a
> On 12/26/05, Jordan Tiona <to*******@comcast.net> wrote:
Jordan Tiona wrote:
Do not top-post, rearranged.
I can't get this code to work right. It seems to be skipping some of the
cin
functions. Can someone help me with this?
http://www.parashift.com/c++-faq-lit...t.html#faq-5.7
http://www.parashift.com/c++-faq-lit....html#faq-15.6
http://www.parashift.com/c++-faq-lite


Sorry, and thank you. One last thing. Is one not allowed to have symbols in
a char array?


A char array does not make any distiction from what it contains. It
does not contain "symbols" but integral values. What these values
represent depend on the underlying system.
For class name, I typed C++, and it ended the EnterData
function and did an infinite loop in main.


Are you sure it's a class name you entered and not a class id? Use
your debugger to check if some input statements are skipped. To quote
from the FAQ: "numerical extractor leaves non-digits behind in the
input buffer". Do you input a numerical value before asking for the
class name?
Jonathan

Dec 26 '05 #4

P: n/a

"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
On 12/26/05, Jordan Tiona <to*******@comcast.net> wrote:
> Jordan Tiona wrote:
Do not top-post, rearranged.
>> I can't get this code to work right. It seems to be skipping some of
>> the
>> cin
>> functions. Can someone help me with this?
>
> http://www.parashift.com/c++-faq-lit...t.html#faq-5.7
> http://www.parashift.com/c++-faq-lit....html#faq-15.6
> http://www.parashift.com/c++-faq-lite
>


Sorry, and thank you. One last thing. Is one not allowed to have symbols
in
a char array?


A char array does not make any distiction from what it contains. It
does not contain "symbols" but integral values. What these values
represent depend on the underlying system.
For class name, I typed C++, and it ended the EnterData
function and did an infinite loop in main.


Are you sure it's a class name you entered and not a class id? Use
your debugger to check if some input statements are skipped. To quote
from the FAQ: "numerical extractor leaves non-digits behind in the
input buffer". Do you input a numerical value before asking for the
class name?
Jonathan


Sorry again. Ok, so here is my newest code.

ClassTrack.cpp:

#include <iostream>
#include "ClassTrack.h"
using namespace std;

const MAX_CLASSES = 12;
ClassType classes[MAX_CLASSES];
int classIndex;

int main(){
classIndex = 0;
bool quit = false;
int choice;

do {
system("cls");

cout << "Welcome to Class Tracker. What would you like to do? \n";
cout << "\t(1) Enter new Class Data \n";
cout << "\t(2) Print Class Data \n";
cout << "\t(3) Quit \n";

cin >> choice;

switch(choice){
case 1: EnterData();
break;
case 2: PrintData();
break;
case 3: quit = true;
break;
default: cout << "\nThat is an invalid choice. Please try again\n";
system("cls");
break;
}
}while(quit == false);

return 0;
}

void EnterData(){
int choice;

system("cls");
cout << "How many classes do you wish to enter?\n";
cin >> choice;

for(int i = 0; i < choice; i++){
cin.ignore();
cout << "\n Name of class:";
cin.getline(classes[classIndex].nm, 25);
cout << "\n Class ID:";
cin >> classes[classIndex].id;
cin.ignore();
cout << "\n Class meets on:";
cin.getline(classes[classIndex].meets, 7);
cout << "\n Starting Time:";
cin.getline(classes[classIndex].start, 10);
cout << "\n Ending Time:";
cin.getline(classes[classIndex].end, 10);
cout << "\n Teacher:";
cin.getline(classes[classIndex].teacher, 25);
cout << "\n Number of students:";
cin >> classes[classIndex].numStudents;

classIndex++;
}
}

void PrintData(){
char choice[4];

system("cls");
cout << "Type the class ID of the class that you want\n";
cout << "to view, or type 'all' to print all of them.\n";
cin.getline(choice, 3);
cin.ignore();

if(choice == "all"){
for(int i = 0; i < classIndex; i++){
cout << classes[i].nm << endl;
cout << classes[i].id << endl;
cout << classes[i].meets << endl;
}
}
else;
}

ClassTrack.h:

void EnterData();
void PrintData();

struct ClassType {
char nm[25];
int id;
char meets[7];
char start[10];
char end[10];
char teacher[25];
int numStudents;
};

Sorry about the spacing. Anyway, When testing it out, I enter the data for a
class. Then when I select "Print Data", and type "all", it goes back to
main, prints the menu, then goes straight back to PrintData, and does this
over and over again. What am I doing wrong?
Dec 26 '05 #5

P: n/a
pH
There are a few problems in that, mainly with the cin buffer getting
stuff left in it.

You need a cin.ignore() after the cin >> choice; in main() as otherwise
the CR/LF isn't flushed and is seen by cin.getline() in printdata as
the end of the string, so you effectively never enter anything, and
when you get back to cin >> choice, there is a CR/LF left in your
buffer, so it leaves choice as it was (as it can't parse the buffer as
an int), i.e. set to 2, so printdata is called again, repeatedly. You
may also find you need cin.ignore() at the end of EnterData for a
similar reason.

In PrintData, you have cin.getline(choice, 3); but the length parameter
includes the terminating NULL character, so you only read two 'real'
characters - 'al', not 'all'. So, change the 3 to a 4.

Also, you shouldn't need cin.ignore() at this point [may depend on your
STL version], as getline should flush the terminating CR/LF from the
stream. You could however put cin.ignore(); at the end of this
function, so you can see its output before pressing a key to return to
main for the next command.

Then, you compare choice == "all". This won't work as choice is just a
pointer to the text data, and "all" is also compiled to a pointer to
some fixed data "all\0"; so, the == is comparing the pointers, i.e. is
true if they point to the same place, not to data that has equal bytes.
Instead, use strcmp(choice, "all"); that'll compare the character
strings themselves and returns 0 if they're equal - so invert it to
give the condition, i.e. if (!strcmp(choice,"all")) {...}.

....and then it might work :D

Dec 27 '05 #6

P: n/a
Thank you so much PH. One last Q. How can I convert a character array into
an unsigned Byte?
Dec 27 '05 #7

P: n/a
pH
Well, it depends exactly what you mean by that. A 'char' is one byte
wide (it's just numeric, storing an ASCII code, not a 'proper'
Unicode/etc character), and probably unsigned [though it might be
signed, depending on your compiler]. To convert it to an unsigned
'byte' *array*, use (unsigned char *)my_char_array;.

If on the other hand you mean you want to turn an array of numeric
characters into actual numbers, you just need to subtract '0' [i.e. the
ASCII code for zero, 48] from each element.

It'd be helpful to know exactly what you're trying to do...

Dec 27 '05 #8

P: n/a
pH
Oh, if you meant how to convert the text representation of a number
"123" to its numerical form 123, then you could use strtol, and cast
down to unsigned char, like
unsigned char num_val = (unsigned char)strtol(char_buffer, NULL, 10);.

The second two parameters are described in various bits of
documentation; basically, the second can be a pointer to a pointer to
receive the address of the first non-parseable character, and the third
is the base.

Dec 27 '05 #9

P: n/a

Jordan Tiona wrote in message ...
Thank you so much PH. One last Q. How can I convert a character array into
an unsigned Byte?


You can not!! Can you put a library on one piece of paper? Can you put a
gallon of milk in one teaspoon? (uhhh, I mean outside of a black hole. <G>).
char Achar( -7 );
unsigned char AUchar( Achar );
int Ichar( Achar );
int IUchar( AUchar );
std::cout<<"Ichar="<<Ichar<<" IUchar="<<IUchar<<std::endl;
// output: Ichar=-7 IUchar=249

--
Bob R
POVrookie
Dec 28 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.