473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

I'm having trouble. Help?

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
9 1730
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
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
> 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

"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
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
Thank you so much PH. One last Q. How can I convert a character array into
an unsigned Byte?
Dec 27 '05 #7
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
1
by: Lauren Wilson | last post by:
I'm having trouble with the Access VBA help on my installation of A2K with Dev tools. Every time I try to retrieve help for items listed in the Object Browser (and SOME other items as well),...
2
by: Jozef | last post by:
Hello, I am trying to put together a module and open a workspace on a database that has a simple password (using Access XP). This is the lin that I'm having trouble with; Set wrk =...
0
by: Jozef | last post by:
Hello, I'm having trouble with the download links on my web server. The error I'm getting is; CGI Timeout The specified CGI application exceeded the allowed time for processing. The server...
1
by: Jozef | last post by:
Hello. I'm having trouble creating a blank solution (and ASP.net web application) from my laptop. I own the server (in fact it's sitting right next to me) and have added the URL to the trusted...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
12
by: Fozzi | last post by:
Hey all I have been working on a project which allows me to write to a file and retrieve from that file, but i having trouble with it These are my inputs: private int flightno; ...
1
by: Alexnb | last post by:
Okay, what I want to do with this code is to got to thesaurus.reference.com and then search for a word and get the syns for it. Now, I can get the syns, but they are still in html form and some are...
1
by: runningsnake24 | last post by:
We are writing a program to check that a filled in Sudoku puzzle is solved correctly. We are required to use the Iterator and Iterable interfaces. The program uses a Cell class to represent each...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.