473,788 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cla sses[classIndex].nm, 25);
cout << "\n Class ID:";
cin >> classes[classIndex].id;
cout << "\n Class meets on:";
cin.getline(cla sses[classIndex].meets, 7);
cout << "\n Starting Time:";
cin.getline(cla sses[classIndex].start, 10);
cout << "\n Ending Time:";
cin.getline(cla sses[classIndex].end, 10);
cout << "\n Teacher:";
cin.getline(cla sses[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(cho ice, 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 1751
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.goo glegroups.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*******@comc ast.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******** *************@g 49g2000cwa.goog legroups.com...
On 12/26/05, Jordan Tiona <to*******@comc ast.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(cla sses[classIndex].nm, 25);
cout << "\n Class ID:";
cin >> classes[classIndex].id;
cin.ignore();
cout << "\n Class meets on:";
cin.getline(cla sses[classIndex].meets, 7);
cout << "\n Starting Time:";
cin.getline(cla sses[classIndex].start, 10);
cout << "\n Ending Time:";
cin.getline(cla sses[classIndex].end, 10);
cout << "\n Teacher:";
cin.getline(cla sses[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(cho ice, 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(cho ice, 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(cha r_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<<"Ich ar="<<Ichar<<" IUchar="<<IUcha r<<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
2590
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 access the recordset it displays the results, what the real issue is that the entry is not made into the database even though i use the Update command and i have also tried the BeginTrans and CommitTrans nothign seems to work and i am unable to...
1
1801
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), Access tells me that the "feature is broken" and prompts me to repair it. When I do so, it goes through the motions but does NOT repair the help files. Anyone have a clue about this problem?
2
4549
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 = CreateWorkspace("TestWrkspc", "Admin", conDbPwd) conDBPwd is a variable that contains the password. There is no independant workgroup file, just the default. This is the error message I'm getting;
0
1540
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 has deleted the process. It's a fresh Windows 2000 server install, but I also installed the ASP.net
1
1639
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 sites on my laptop. Here are the details; This is what I'm selecting from the start page.... >Add New Blank Solution >Visual Basic Projects,
0
1851
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 add only new records and not all the records. As an example, this query filters a list of property owners for duplicates and then appends to a table where they are assigned autonumer IDs. The problem is that I'm constantly entering new owners in and...
0
1720
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 I want the latest comment to be on top, and that's where I'm running into trouble. Since I know very little about PHP, I thought I was clever in what I came up with. I think it can work if I get the coding right. Let me know if my logic is wrong. I'm...
12
1935
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; private int sourceairportcode; private int destinationaiportcode; private String takeofftime; private String landingtime;
1
1374
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 hyperlinks. But I can't get the contents out. I am not that familiar with BeautifulSoup. So if anyone wants to look over this code(if you run it, it will make a lot more sense) and maybe help me out. side note: if you run it, a list object...
1
3268
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 individual cell of a puzzle, and uses a 2D array to represent the entire puzzle. The next method in our class that implements Iterator must return a Cell object array that stores the current row, column and box that we are checking. I have figured...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.