Does my source code shows my own understanding of structures in c++?
I'd like to know if I am in fact in the right direction, I am currently learning the C++ language and reading this book called Jumping into C++ by Alex Allain and there's a practice problem at the end of the chapter regarding structures, to create a contact book program the user should be able to not just fill out a single structure, but should be able to add new entries, each with a separate name and phone number. Let the user add as many entries as he or she wants—is this easy to do? Add the ability to display all, or some of the entries, letting the user browse the list of entries.
so far below is what I've done, I'd like to know if my source code is in fact right and does it show my understanding about structures and overall c++?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
struct user{
string name;
int phone_num;
};
int _tmain(int argc, _TCHAR* argv[])
{
int input, number; // will hold the users input at the beginning of the program
int counter = 0; // keep track of the array position
int const arraySize = 10; // size of the array
user new_username[arraySize]; // will hold the users details
string name; // will hold the users input for the name
cout << "CONTACTSn";
do{
cout << "+ADD [1] -EXIT[0]";
cin >> input;
if(input == 1){
//cout << counter;
cout << "nName: ";
cin >> name;
new_username[counter].name += name;
cout << endl << "nPhone: ";
cin >> number;
new_username[counter].phone_num = number;
counter++;
//set_user(counter);
}
cout << "Name Numbern";
cout << "--------------n";
for(int j=0; j < arraySize; j++){
cout << new_username[j].name;
cout << " -- ";
cout << new_username[j].phone_num;
cout << "n";
}
cout << "n";
}while(input != 0);
cout << "n";
system("PAUSE");
return 0;
}
If you want the user to be able to add as many contacts as he/she wants, you can use powerful standard template mechanisms.
For this application, I would recommend looking at either
std::vector
or
std::map
This is how you would use them: (keep in mind this is pseudo code and won't compile)
#include <vector>
typedef struct {
std::string name;
double phoneNumber;
} YourStruct;
int main( int argc, char **argv ) {
std::vector<YourStruct> structVector;
do {
int input;
std::cin >> input;
if (input) {
//(read user input for name and number)
YourStruct yourStruct;
yourStruct.name = (user input)
yourStruct.phoneNumber = (user input)
// insert into the vector
structVector.push_back(yourStruct)
}
} while (input != 0)
// now to print what you have:
for (int i = 0; i < structVector.size(); i++) {
std::cout << structVector[i].name << ", " << structVector[i].number << std::endl;
}
}
The benefit to using vectors is that it automatically resizes and keeps track of how large it is without you having to use a counter item.
Now, for something a bit trickier. We're going to use a map to use the "key" value to get the name. The following code won't compile but it is functionally how you would perform the task:
#include <map>
int main(int argc, char** argv) {
std::map<std::string,double> myMap;
// the string is the "key" value, which can be the name of the person
// while the "independent" is the phone number
do {
// determine if the user wants to put another entry in the map
if (insert) {
std::string name = (user input name)
double number = (user input number)
myMap[name] = number;
}
} while (input != 0)
// now we can iterate through the map
std::map<std::string,double>::iterator it;
for (it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it.first << ", " << it.second << std::endl;
}
// also, you can look up by name
it = myMap.find("Tony Stark");
if (it != myMap.end()) { // if this condition is met, it means you found one
std::cout << it.first << ", " << it.second << std::endl;
}
}
Overall, your code looks good. However, it is not C++. You're programming like a C programmer. The beauty of C++ (besides polymophisim, of course) is the powerful template libraries.
I've just given you a small taste of what you can do with templates. Please comment if you have any questions of concerns. We've all been where you are, kudos for teaching yourself out of a book.
Stackoverflow isn't meant to be used for code reviews, but there's a different site for this (although still in beta): https://codereview.stackexchange.com/
Just some quick things I noticed:
user
array is full. As for basic understanding... I guess yes, but that's not actually hard to start with.
To fulfill "allow the user to add as many entries as they want" you'll have to use a dynamic array (ask the user how many entries he'd like to add) or use some dynamic storage (eg a linked list).
From your question and the code it seems like you are a new programmer, therefore I'll explain you the answer and I'll give you some notes on your code.
In order to solve the problem of "as many items" there are few approaches. The most easy one, and probably a pretty good solution is to use map, in any language it have different names. But usually the name is dictionary, associative arrays...
Using the map will help you with dealing with:
The other approaches I talked about i though of, are much more basic, and consist much more code, but they give you the feeling of how you can implement the map object by yourself, but this is a different question.
In the link I mention above, the example is for phone entry. But if you still want to use struct, you can have the key as the name and the value to be the struct itself. One justification for that can be that later on you plan to add address and company name.
Some notes regarding your code.
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
//use meanigful name, instead of user it can be phoneEntry
struct user{
string name;
//Starting using the following conventions using the capital letter in variable names for example phoneNumber
//int won't be god for phone number since some times you will need star or hash, or a leading zero, maybe a plus sign. It is better touse string for tat as well. And of course every time that you get a user input you should validate it int phone_num; };
//Why the name of the function is not main, why its _tmain
int _tmain(int argc, _TCHAR* argv[])
{
//Keep going with your comments, with time you would imbrase your own style based on things that you will see. But in general commenting is very important
//Give meanigful name, instead input userCommand for example
int input, number; // will hold the users input at the beginning of the program
int counter = 0; // keep track of the array position
int const arraySize = 10; // size of the array
//Again meangful names
user new_username[arraySize]; // will hold the users details
string name; // will hold the users input for the name
cout << "CONTACTSn";
do{
cout << "+ADD [1] -EXIT[0]";
cin >> input;
if(input == 1){
//cout << counter;
cout << "nName: ";
cin >> name;
new_username[counter].name += name;
cout << endl << "nPhone: ";
cin >> number;
new_username[counter].phone_num = number;
counter++;
//set_user(counter);
}
cout << "Name Numbern";
cout << "--------------n";
for(int j=0; j < arraySize; j++){
cout << new_username[j].name;
cout << " -- ";
cout << new_username[j].phone_num;
cout << "n";
}
cout << "n";
}while(input != 0);
cout << "n";
system("PAUSE");
return 0;
}
I hope it helped
链接地址: http://www.djcxy.com/p/40280.html上一篇: 文字字符串和函数返回值是左值还是右值?