Validate if User Entered a String Value

I am building a program for customer accounts. I have a setCustomer() that allows the user to enter information for each customer. I am attempting to validate that the user entered information for each record using a while loop. I get an Error: invalid conversion from 'char*' to 'int' [-fpermissive]

This is the code for the while loop:

1
2
3
4
5
6
do{ 
           cout<<"\nEnter Data for the Customer:\nName:\t\t\t";
	       cin.getline(customer.name,NAME_LEN);
	       if(isblank(customer.name))
			cout<<"Name cannot be empty!\n";
         }while(isblank(customer.name));


And the complete program code:

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;

const int NAME_LEN   = 45;
const int ADDR_LEN   = 45; 
const int CITY_LEN   = 20; 
const int STATE_LEN  = 45;
const int ZIP_LEN    = 6;
const int PHONE_LEN  = 15; 
const int ARRAY_SIZE = 11;
const int DATE_SIZE  = 30;

//Data structure for creating records
struct Account
{
	char name[NAME_LEN];     // customer name 45 characters
	char address[ADDR_LEN];  // address 45 characters
	char city[CITY_LEN];     // city 20 characters
	char state[STATE_LEN];   // State two letters
	char zip[ZIP_LEN];       // zip 5 numbers
	char tele[PHONE_LEN];    // telephone number 12 characters
	char date[DATE_SIZE];    // date 30 characters
	int  total;              // total array length
	double account;          // account total
};

//Function Prototypes
void displayMenu();
void setCustomer();
void displayAll();
void searchRecords(Account&);

bool rnew = 0;
bool mod  = 0;

int main()
{  
  Account customer;
  
  long fpos;
   
  // Constants for menu choices
  const int ENTER_RECORD   = 1,
            DISPLAY_RECORD = 2, 
            DELETE_RECORD  = 3,
            CHANGE_RECORD  = 4,
            DISPLAY_ALL    = 5;
  
  int    userChoice = 6;
  double filePosition;
 	
      do
      {
        displayMenu();
       
            //Get the users choice
   	        do
   	        {
   	   	     cout << "Enter Your Choice (1-6): ";
		     cin >> userChoice;
	        }
	        while (userChoice < 1 || userChoice > 6);
	
	    //Process userChoice
	    switch(userChoice)
	    {
	       //*******************Enter Records to File*******************
		   case ENTER_RECORD:
           cin.get();
           cout << "\nYou selected Enter a new Customer Account.\n\n";
           rnew = 1;
           setCustomer();
           rnew = 0;
           break;
   
           
           //******************Display a Particular Account*************
           case DISPLAY_RECORD:
           cin.get();
           cout << "\nYou selected Display a Customer Account.\n\n";
           rnew = 1;
           searchRecords(customer);
           rnew = 0;
           break;
         
            
           //******************Delete a Particular Account***************  
           case DELETE_RECORD:
           cout << "\nYou selected Delete a Customer Account.\n\n";
           break;

              
           //******************Change a Particular Account***************  
          

           //******************Display All Accounts**********************
           case DISPLAY_ALL:
           cin.get();
           cout << "\nYou selected Display All Accounts.\n\n";
           rnew = 1;
           displayAll();
           rnew = 0;
           break;
           
           
           //*****************Anything not between 1-5********************
           default: 
           break;
	    }//End of switch statement
	
      }
      while (userChoice != 6);
 
 return 0;
}//End of function 


void displayMenu()
{
  cout << "\n * * * * A C C O U N T  M E N U * * * * \n\n";
  cout << "1. Enter a New Customer Account\n";
  cout << "2. Display a Customer Account\n";
  cout << "3. Delete a Customer Account \n";
  cout << "4. Change a Customer Account\n";
  cout << "5. Display All Accounts\n";
  cout << "6. Exit the Program\n\n";
}


void setCustomer()
{
	Account customer;  //To hold info about a customer
	
	//Opens file for binary output
	fstream accounts ("accounts.dat",ios::out | ios::in | ios::app | ios::binary);
	
	char again; //To hold choice

 do
   { 
	   do{ 
           cout<<"\nEnter Data for the Customer:\nName:\t\t\t";
	       cin.getline(customer.name,NAME_LEN);
	       if(isblank(customer.name))
			cout<<"Name cannot be empty!\n";
         }while(isblank(customer.name));
	   
	   cout<<"Address:\t\t";
	   cin.getline(customer.address,ADDR_LEN);
	   cout<<"City:\t\t\t";
	   cin.getline(customer.city,CITY_LEN);
	   cout<<"State:\t\t\t";
	   cin.getline(customer.state,STATE_LEN);
	   cout<<"Zip Code:\t\t";
	   cin.getline(customer.zip,ZIP_LEN);
	   cout<<"Telephone Number:\t";
	   cin.getline(customer.tele,PHONE_LEN);
	   cout<<"Date of last Payment:\t";
	   cin.getline(customer.date,DATE_SIZE);

	
	
	do{
		cout<<"Account Balance:\t";
		cin >> customer.total;
		cin.ignore(); //Skip remaining newline character
		if(customer.total<0)
			cout<<"Invalid Balance!\n";
	}while(customer.total<0); //data validation
	//write new record*/
	accounts.write(reinterpret_cast<char *>(&customer),sizeof(customer));
	
	//Add another file?
	cout << "\nDo you want to enter another record? \n";
	cout << "\nEnter Y for yes or N for no: " ;
	cin  >> again;
	cin.ignore(); //Skip remaining newline character
  } while (again == 'Y' || again == 'y');
  
	//close file
	accounts.close();
}

void displayAll()
{
 Account customer;
 int count = 0;

 //Opens File for Binary Input
 fstream accounts ("accounts.dat", ios::in |ios::binary);
    if (accounts.fail())
    {
     cout << "\nError opening file. \n";
     return;
    }
    cout << "\nBegining of Customer Records***\n\n";

  while (accounts.peek()!=EOF)
  {
   accounts.read(reinterpret_cast<char*>(&customer), sizeof(customer));
   cout << setprecision(2);
   cout << fixed << showpoint;
   cout << "\nRECORD NUMBER " << ++count << ":" << endl;
   cout << "\nCustomer Name: " << customer.name << endl;
   cout << "Customer Address: " <<customer.address << endl;
   cout << "City: " << customer.city << endl;
   cout << "State: " << customer.state << endl;
   cout << "Zip: " << customer.zip << endl;
   cout << "Telephone: " << customer.tele << endl;
   cout << "Date of Last Payment: " << customer.date << endl;
   cout << "Account Balance: " << customer.total << endl;
   cout << endl;
   cout << "\nPress Enter to Continue...";
   cin.get();
  }
    if (count == 0)
    {
     cout << "\nFile is empty." << endl;
    }
  cout << "\nEnd of Customer Records\n\n";
  accounts.clear();
  accounts.close();
}


void searchRecords(Account &sRecord) //Customer is the name of the structure
{
   bool foundIt = false;
   int record = 0;	      //Holds record location
   string searchName;	  //String for input
   fstream customerFile;  //Creates stream
      
   customerFile.open("accounts.dat", ios::in | ios::out | ios::binary);       //Opens file binary
   if(!customerFile.fail())
   {
   cout << "Enter name to search: ";
   getline(cin, searchName);	//Gets input
   
   while(!customerFile.eof())	//While loop to search through file
   {
     customerFile.seekg((sizeof(Account) * record), ios::beg);	              //Moves reading location
     customerFile.read(reinterpret_cast<char *>(&sRecord), sizeof(sRecord));  //Reads current location
     
     if(sRecord.name == searchName)
	      {
        foundIt = true;		//Displays if the sRecord.name matches the searchName
        
         cout << fixed << showpoint;
         cout << "\nCustomer name: "  << sRecord.name << endl;
         cout << "Customer Address: " << sRecord.address << endl;
         cout << "City: "  << sRecord.city << endl;
         cout << "State: " << sRecord.state << endl;
         cout << "Zip: "   << sRecord.zip << endl;
         cout << "Telephone: " << sRecord.tele << endl;
         cout << "Account Balance: " << sRecord.total << endl;
         cout << "Date of Last Payment: " << sRecord.date << endl;
		     
        customerFile.seekp((sizeof(Account) * record), ios::beg);                 //Move writing location
        customerFile.write(reinterpret_cast<char *>(&sRecord), sizeof(sRecord));  //Writes new information to current location
        return;
        
     }
     
     record++;
     
   }
   customerFile.close();	//Displays if no match is found
   if(foundIt == false)
   cout << "No record found" << endl;
   }
   else
   cout << "Issue with file:" << endl;
}
http://www.cplusplus.com/reference/cctype/isblank/


Read this documentation :+) and you will see why you have a problem.

Also, don't loop on eof. Loop on getline or the stream itself.

Can you use std::string ? That would be better than char arrays.


In function 'int main()':
45:8: warning: unused variable 'fpos' [-Wunused-variable]
51:13: warning: unused variable 'CHANGE_RECORD' [-Wunused-variable]
55:10: warning: unused variable 'filePosition' [-Wunused-variable] In function 'void setCustomer()':
149:33: error: invalid conversion from 'char*' to 'int' [-fpermissive]
151:38: error: invalid conversion from 'char*' to 'int' [-fpermissive]

filePosition wasn't used, but double is a bad type for it anyway, consider std::size_t
I read the cplus.com article but we are not using the #include <stdio.h> or
#include <ctype.h> headers. Are these required? I tried a few different ways I guess I am not understanding the syntax of that article.

We can not use std:string

The unused variable error is due to the fact that I had to erase a function and code to stay under the character limit for this post


I read the cplus.com article but we are not using the #include <stdio.h> or
#include <ctype.h> headers. Are these required?


Looks like <ctype.h> or <cctype> are required.


http://en.cppreference.com/w/cpp/string/byte/isblank


I tried a few different ways I guess I am not understanding the syntax of that article.


The isblank function works on a char, you were trying to use it on an array of char.

The unused variable error is due to the fact that I had to erase a function and code to stay under the character limit for this post


But the type is probably wrong, what do you understand double to mean?

You can always do another post to show the rest of the code. :+)

Cheers:+)
Topic archived. No new replies allowed.