473,513 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Probably a stupid question, but...

3 New Member
I have already inserted information (a state's name, abbreviation, year of admission, etc) into a linked list (not shown). Now when I'm trying to find a state in the list, a state that the user inputs, with a seperate retieve function. The problem is I'm trying to put the user input string (the name of the state that I want to be searched for in the list) into a struc so I can pass that structure variable in the retieve function, but I don't know how.

The code below hopefully might clear some things up...
Expand|Select|Wrap|Line Numbers
  1. struct listInfo //struct containting state information
  2. {
  3.     char name [15];
  4.     int population;
  5.     int area;
  6.     int year;
  7.     int order;
  8.     char capital[15];
  9.     char abbrev[3];
  10.  
  11.  
  12. };
  13.  
  14. void reportYrPop (SortedType <listInfo>& list, int density, ostream& output)
  15. {
  16.     listInfo getYrPop;
  17.     string input;
  18.     bool found;
  19.  
  20.     cout <<"Type in state to find year of admission and population: ";
  21.     getline (cin, input);  //get the state I want to look for in the list
  22.  
  23.     getYrPop.name = input;  //I know this is wrong code.  But I want to put the user input (string) into a struct variable (char), so when I call retrieveItem later on below, it'll retieve the correct information from the linked list corrposding with the state that was just inputted by the user.
  24.  
  25. while (!input.empty()) //while user doesn't type an empty string
  26.     {
  27.         list.RetrieveItem(getYrPop,found);  //Searches for an item, with all state information, from the list.  Passes an object and a bool variable as parameters.
  28.  
  29.         if (found) // if the state is in the list
  30.         {
  31.             cout << "Year and Population Density: "<< getYrPop.year << " " << density<<endl;  //prints to screen
  32.  
  33.             output <<"Year and Population Density: "<< getYrPop.year << " "<<density<<endl; //prints to file
  34.  
  35.         }
  36.         else  //if the state was not found
  37.         {
  38.             cout <<"The state was not found"<<endl;
  39.             output << "The state was not found"<<endl;
  40.         }
  41.         cout <<"Type in state to find year of admission and population: ";
  42.  
  43.         getline(cin,input); //keep getting user input until user enters an empty string
  44.     }
  45. }
Sorry if my question is still unclear. I'll add more later if I'm leaving anything out, but any help would be appreciated.
Apr 27 '07 #1
4 1191
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
Expand|Select|Wrap|Line Numbers
  1. getYrPop.name = input;  //I know this is wrong code.  But I want to put the user 
  2.  
is not going to work. input is a C++ string but the getYrPop.name is a 15 byte char array.

Use C++ string throughout and avoid arrays.
Apr 27 '07 #2
LShelby
3 New Member
Thanks for your suggestion, but is there a way to get around this problem without having to change anything in the structure?
Expand|Select|Wrap|Line Numbers
  1. struct listInfo 
  2. {
  3.     char name [15];
  4.     int population;
  5.     int area;
  6.     int year;
  7.     int order;
  8.     char capital[15];
  9.     char abbrev[3];
  10.  
  11.     bool operator < (listInfo lessThan) 
  12.     {  
  13.         return (strcmp (name, lessThan.name) < 0); 
  14.     } 
  15.     bool operator == (listInfo equal)
  16.     { 
  17.         return (strcmp (name, equal.name) == 0);
  18.     }
  19. };
  20.  
I just added some code at the bottom of the structure. When I'm using strcmp, I think the name component has to be a char pointer (or array) down where I initlize new assignment operators (== and <) that I'll be using in another class (which contains the retrieve function). Also I don't think I can add any more variables inside the structure because I'm reading information from a binary file, then inserting each component of the strcture into the list corrposonding with the variables already there in THAT order. So...yah, I'm really starting to think I'm screwed, but again, I really appreciate the help!
Apr 27 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
If you insist on a 15 byte array, then that will hold a 14 character C-string. You have to copy not more than 14 characters to that array:

Expand|Select|Wrap|Line Numbers
  1. if (input.size() < 15)
  2. {
  3.     strcpy(getYrPop.name, input.c_str())
  4. }
  5. else
  6. }
  7.     strncpy(getYrPop.name, input.c_str(), 14);
  8.     getYrPop.name[14] = '\0';
  9. }
  10.  
You do realize, I hope that the C++ string object is a replacement for a C char array. You really should have a string as the class member.
Apr 28 '07 #4
LShelby
3 New Member
All right, that did the trick. I can't thank you enough! :)
Apr 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

119
4510
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
5
3125
by: raz | last post by:
Greetings all. I apologize for what is almost certainly a stupid question, but I can't figure this out, and have no more time for head bashing... The short version: what is the appropriate...
9
1560
by: noone | last post by:
I have a database file that I use an autonumber field as the primary key index. Because of some rearrangements in the past, this index does not match the order that I would like it to be in, that...
3
7103
by: Catweasel | last post by:
I'm new to C++ and have been chucked in at the deep-end. I have a C++ console app that works fine. All I want to do is write to file however as soon as I include the fstream library and try to...
2
1285
by: Ron Weldy | last post by:
I read that you don't need .cs files to deploy but I suppose if you are trying to reconstruct someone's work, you will need these files. Is that correct?
6
1598
by: Adam Smith | last post by:
I have posted this and similar questions repeatedly and can't even raise a single response. I am being led to believe that this then 'Must be a stupid question' although people say that there is no...
1
5858
by: Stef Mientki | last post by:
Does anyone know the equivalent of the MatLab "diff" function. The "diff" functions calculates the difference between 2 succeeding elements of an array. I need to detect (fast) the falling edge of...
2
1511
by: Lynx101 | last post by:
Hi, Is this a stupid question? Senario: Two tables linked together with an ID number. Question: When using a combo box, which refences another table by indexed autonumber, is there a way to...
9
1598
by: Alec | last post by:
Sorry guys, stupid question.... Am no programming expert and have only just started using php for creating dynamic news pages. Then I see a dynamic website without the php extension. ...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7537
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...
1
7099
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...
0
5685
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,...
1
5086
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...
0
3233
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
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 ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.