I'm having trouble. Help?

J

Jordan Tiona

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.nm << endl;
cout << classes.id << endl;
cout << classes.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.
 
J

Jordan Tiona

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.
 
J

Jonathan Mcdougall

Do not top-post, rearranged.
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
 
J

Jordan Tiona

Jonathan Mcdougall said:
Do not top-post, rearranged.


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.


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.nm << endl;
cout << classes.id << endl;
cout << classes.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?
 
P

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
 
J

Jordan Tiona

Thank you so much PH. One last Q. How can I convert a character array into
an unsigned Byte?
 
P

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...
 
P

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.
 
B

BobR

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top