Using varibles to create new object.

Hello, is it posbile to create new object from variable string nameOnList and number wich would be a passed into constructor .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
int menu();
int selection;

int main()
{
	while (true)
	{
		selection = menu();
		if (selection == 1)
		{
			int number;
			std::cout << "Please enter name of the conntact" << std::endl;
			std::string nameOnList;
			std::cin >> nameOnList;
			std::cout << "Please enter the number: " << std::endl;
			std::cin >> number;
			List nameOnList(number);
		}
		else
		{
			std::cout << "Computer is working hard" << std::endl;
		}
		
	}

	std::string nameOnList;
	std::cin >> nameOnList;


	system("pause");
	return 0;

}

int menu()
{
	std::cout << "Please enter only one letter to make your choise!" << std::endl;
	std::cout << "***** Telephone List *****\n"
		<< "D = Display all entries\n"
		<< "F = Find a telephone number\n"
		<< "A = Append an entry\n"
		<< "E = Erase an entry\n"
		<< "Q = Quit the program\n";
	char choice;
	std::cin >> choice;
	if (choice == 'D' || choice == 'd')
	{
		return 1;
	}
	else if (choice == 'f' || choice == 'F')
	{
		return 2;
	}
	else
	{
		std::cout << "Wrong input" << std::endl;
		menu();
	}
}


//////telephoneList.h
#pragma once
#include <iostream>
#include <string>
class List 
{
private:
	std::string name;
	int phoneNumber;
public:
	List( int nr);
};
/////telephoneListMethods
#include <iostream>
#include <string>
#include <cmath>
#include "telephoneList.h"

List::List( int nr)
{
	phoneNumber = nr;
}
Last edited on
Hello MrMode,

First off I would make "phoneNumber" in the class a string and any place else where it is used because you never know how someone may enter a phone number unless you give an example of what you want in the prompt.

In line 17 of main the use of new is not needed and I believe what you have is the wrong way to phrase that line. "List nameOnList(number);" will create one object of type "List" with the phone number entered.

Inside the while loop in "main" a switch/case would be a better choice than the if/else statements. Also the switch/case would work better in the "menu" function with the function returning a "char" instead of the "int".

I will need a little time to test the menu function and make some changes for a better use.

As soon as I bave better answers I will let you know.

Hope that helps,

Andy
Yes, assuming you've defined a constructor that takes those arguments. Looking at your List class, you've yet to do that - you have a constructor that takes only one of those things as an argument.

Your line 17 makes no sense. Do you understand what the new keyword is for, and how to use it?
Yes gonna delete new, I just left it when i was trying out few fixes i read on other forum, sorry about that.
I am trying to make that object name would be taken from variable nameOnList, and that object would be called on what user has entered, if he entered Name user1, object would be called user1, and then he has to enter phone number wich would be added as a parameter. so constructor has only to take and set 1 variable.
Oh, so you're asking about reflection:

https://en.wikipedia.org/wiki/Reflection_(computer_programming)

No, you can't do that in C++. The closest you can get is to use something like a std::map that associates the name specified by the user, with the object.
Thanks, gonna need to try some other approach then.
You're welcome - hope it helped.
Hello MrMode,

After working with the program I have come up with for the "menu" function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
char menu()
{
	char choice;
	bool cont{ true };

	do
	{
		std::cout << "Please enter only one letter to make your choise!" << std::endl;
		std::cout << "***** Telephone List *****\n\n"
			<< "D = Display all entries\n"
			<< "F = Find a telephone number\n"
			<< "A = Append an entry\n"
			<< "E = Erase an entry\n"
			<< "Q = Quit the program\n"
			<< "Enter choice: ";
		std::cin >> choice;

		choice = std::toupper(choice);  // <--- Or you could add case statements for lower case letters.

		switch (choice)
		{
			case 'D':
			case 'F':
			case 'A':
			case 'E':
			case 'Q':
				cont = false;
				break;
			default:
				std::cout << "\n  Invalid choice. Try again.\n\n";
				break;
		}
	} while (cont);

	return choice;
}


I also noticed that choice "D" is to "display all entries", but in main you are using it to create an object of type "List" and display nothing.

I think what you want is to first create an array of type list or better a vector to hold information and populate it before you display all entries.

As MikeyBoy has pointed out your overloaded ctor should take two parameters not just the phone number.

In "main" I found a problem with "nameOnList" being defined as a string for input and then again as type "List". This is not allowed. I had to change the string to just name for it to work.

What is in "main" needs to be reworked to accomplish what you want based on the menu choices and I think I would put at the top of the list of menu choices.

As a hint I think it looks nicer to end a prompt with ": " and leave off the "std::endl" so that the "std::cin" will follow on the same line.

Again if you want to leave the variables for phone numbers as an "int" you should give an example of how to enter the phone number in the prompt so the user will know what to enter.

Hope that helps,

Andy
Topic archived. No new replies allowed.