return for a function

I need the function char* getString(char* prompt) to return the name address and phone number. I cant figure out how to get it to return to the function and get the next information it needs. (ex It asks for name first then address then phone)

how do I get it to return to function get the address without asking all three questions again.

Also how do I get it to return name, then address, then phone


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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <cstring>
#include <cctype>
#include "graph1.h"

using namespace std;

//Prototypes are below:
bool validateName(char* name);
char* getString(char* prompt);
bool validateAddress(char* address);
bool validatePhone(char* phone);
char* convertToUpper(char* string);
void displayFields(char* name, char* address, char* phone);

int main()
{
  //Variable Declaration/Initialization
  char* name = NULL;
  char* address = NULL;
  char* phone = NULL;
  char* upperName = NULL;
   
  bool res = false;
  
 

  //Display graphics window
  displayGraphics();

  //Get the fields - validate based on function
  do
  {
    //Get the name
    name = getString("Enter Name: ");

    //validate the name
    res = validateName(name);

  }while(!res);

 do
  {
    //Get the address
    address = getString("Enter Address: ");

    //validate the address
    //res = validateAddress(address);

  }while(!res);
  
  do
  {
    //Get the phone
    phone = getString("Enter Phone: ");

    //validate the phone
    res = validatePhone(phone);

  }while(!res);

 /* cout << endl << endl << endl;

  //Convert the name to all uppercase
  upperName = convertToUpper(name);
 
  //Display the fields
  displayFields(upperName,address,phone);*/

  delete[] name;
  delete [] address;
  delete[] phone;
  return 0;

}


//Function implementation goes here

bool validateName(char* name)
{
	
	//variable declaration and initialization
	int i = 0;

	for (i = 0; name[i] != NULL; i++)
	{
		//if not an alpha
		if (isalpha(name[i]) == 0)
		{
			//if not a space
			if (isspace(name[i]) == 0)
			{
				return(false);
			}
		}
	}
	return(true);

}
char* getString(char* prompt)
{
	
	char buffer [255];
	int i;
	
	
	char* name=NULL;
	cout<<"Enter Name: ";	
	cin.getline(buffer, 255);
	name = new char[strlen(buffer) + 1];
	strcpy(name,buffer);

    char* address=NULL;
    cout<<"Enter Address: ";
	cin.getline(buffer,255);
	address= new char[strlen(buffer) +1];
	strcpy(address,buffer);
	

	char* phone = NULL;
	cout<<"Enter Phone: ";
	cin.getline(buffer,255);
	phone= new char[strlen(buffer) +1];
	strcpy(phone,buffer);
	
	
}
	

bool validateAddress(char* address)
{
		int i = 0;

	for (i = 0; address[i] != NULL; i++)
	{
		//if not an alpha
		if (isalnum(address[i]) == 0)
		{
			//if not a space
			if (isspace(address[i]) == 0)
			{
				return(false);
			}
		}
	}
	return(true);

}

bool validatePhone(char* phone)
{
	int i = 0;

	for (i = 0; phone[i] != NULL; i++)
	{
		//if not an alpha
		if (isdigit(phone[i]) == 0)
		{
			
				return(false);
			
		}
	}
	return(true);

}

/*char* convertToUpper(char* string)
{
}
void displayFields(char* name, char* address, char* phone)
{
}
*/
Why are you trying to write C in C++?
what do you mean? its for school and he gave us a basic outline of this.
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
#include <iostream>
#include <cstring>
#include <cctype>
#include "graph1.h"

using namespace std;

//Prototypes are below:

int main()
{
  //Variable Declaration/Initialization
  char* name = NULL;
  char* address = NULL;
  char* phone = NULL;
  char* upperName = NULL;
  bool res = false;
  
 

  //Display graphics window
  displayGraphics();

  //Get the fields - validate based on function
  do
  {
    //Get the name
    name = ____("Enter Name: ");

    //validate the name
    res = ____(name);

  }while(!res);

  do
  {
    //Get the address
    address = ____("Enter Address: ");

    //validate the address
    res = ___(address);

  }while(!res);
  
 do
  {
    //Get the phone
    phone = ____("Enter Phone: ");

    //validate the phone
    res = ____(phone);

  }while(!res);

  cout << endl << endl << endl;

  //Convert the name to all uppercase
  upperName = ______(name);
 
  //Display the fields
  _____(upperName,address,phone);

  return 0;
}

//Function implementation goes here 






we have to use that but we can add to it. he wants us to only use one return function though
Are you allowed to use std::string? If not, you're basically forced to do this the hard (and dangerous) way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char* getString(char* prompt)
{
	//variable declaration and initialization
	char* ret_val = NULL; //declare my return value
	char buffer[255];

	cout << prompt;
	cin.getline(buffer,sizeof(buffer));

	ret_val = new char[strlen(buffer) + 1];

	strcpy(ret_val, buffer);

	return(ret_val);

}
	





that's how it is done is that the hard way?
It will not (should not) compile if you call it with a string literal, and if you neglect to call delete[] on the return value when you're done with it you will have a memory leak.

This is the easy (and correct) way:
1
2
3
4
5
6
7
std::string getString(std::string const &prompt)
{
    std::cout << prompt;
    std::string input;
    std::getline(std::cin, input);
    return input;
}
No memory leaks, can be called with string literals, and is a heck of a lot smaller and easier to read and maintain.
Last edited on
Topic archived. No new replies allowed.