accepting only a pattern input

hi me again..

So in my code i was planning to make a ID in which is pattern as 0000-00-0000(ex. 2014-01-0001)...

so if he inputted something like "2014010001" or random nonsense he will get a message saying "you have entered a incorrect ID PATTERN"

hope my explaining isnt too complicated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <cctype>

bool valid( std::string str )
{
    static const std::string pattern = "0000-00-0000" ;

    for( char& c : str ) if( std::isdigit(c) ) c = '0' ;
    return str == pattern ;
}

int main()
{
    std::string id ;

    std::cout << "enter id: " ;
    std::cin >> id ;
    if( !valid(id) ) std::cout << "you have entered an incorrect ID PATTERN\n" ;
}

ehh having trouble her im getting

C:\Users\Samsung\Desktop\New folder (4)\Cpp1.cpp(12) : error C2530: 'c' : references must be initialized
C:\Users\Samsung\Desktop\New folder (4)\Cpp1.cpp(12) : error C2059: syntax error : ':'
What compiler are you using? You'll want to enable the newer C++ standard features for the range-based for loop used in the example.
im using the visual C++ 6.0
im not obligated to use any other compiler since its the one use at my school

is there any other way, perhaps?
Last edited on
Replace the range-based for loop with a classic for-loop:
1
2
3
// for( char& c : str ) if( std::isdigit(c) ) c ='0' ;
for( std::size_t i = 0 ; i < str.size() ; ++i )
    if( std::isdigit( str[i] ) ) str[i] = '0' ;
i got the error message working but....it wont ask again... :/ the code continues to run the other input being ask..
What do you mean by "the code continues to run the other input being ask"? Post your updated code, too.
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
#include<iostream>
#include<string>
#include<conio.h>
#include<cctype>
using namespace std;

bool valid( string str )
{
    static const string pattern = "0000-00-0000" ;
	for( size_t i = 0 ; i < str.size() ; ++i )
    if( isdigit( str[i] ) ) str[i] = '0' ;
    return str == pattern ;
}

int main()
{
char myinput;
string nam[100],add[100],con[100],cor[100],yel[100];
string search;
string idn[100];
int num_rec,a;

do
{
system("cls");

      cout << "\t\t====== STUDENT INFORMATION SYSTEM ======";
      cout <<"\n\n                                          ";
      cout << "\n\n";
     cout<<" \n\t\t\t======================";
     cout << "\n \t\t\t  [1] Add    Records";
     cout << "\n \t\t\t  [2] Search Records";
     cout << "\n \t\t\t  [3] Modify Records";
     cout << "\n \t\t\t  [4] Delete Records";
     cout << "\n \t\t\t  [5] Exit   Program";
     cout<<" \n\t\t\t======================";
     cout << "\n\n";
     cout << "\t\t\t Select Your Choice :: ";
     cin >> myinput;

switch (myinput)
{

case '1':
{
system("cls");
		
cout<<"** Number of records to be entered (MAXIMUM OF 10) **"<<endl;
cout<<"# ";
cin>>num_rec;
cin.ignore();
if (num_rec < 11)
{
for (a=0; a<num_rec; a++)
{
	
cout<<"Enter your ID number:: ";
getline (cin, idn[a]);
if( !valid(idn[a]) ) cout << "you have entered an incorrect ID PATTERN\n" ;

	cout<<"Enter your Name:: ";
	getline (cin, nam[a]);
	cout<<"Enter your Address:: ";
	getline (cin, add[a]);
	cout<<"Enter your Contact Information:: ";
	getline (cin, con[a]);
	cout<<"Enter your Course:: ";
	getline (cin, cor[a]);
	cout<<"Enter your Year Level:: ";
	getline (cin, yel[a]);
	cout<<"\n\n\n You have Successfully entered the record"<<endl;
}
}
else
{
cout<<" The system cannot handle more than "<<num_rec<<" records"<<endl;
}

cout<<"\n\n\n\n\n************************************************"<<endl;
cout<<"***Please press any key to return to the Menu***"<<endl;
cout<<"************************************************"<<endl;

getch();
break;
}
	
case '2':
{
system("cls");
              
cout<<"** Search and display student's details **"<<endl;
cout<<"Type student's ID NUMBER : ";
cin>>search;
cin.ignore();
	for (a=0; a<num_rec ; a++)
{                   

if (search == idn[a])
{


	  cout<<":: STUDENT ID ::";
	  cout<<idn[a]<<endl;
					  
	  cout<<":: NAME ::";
	  cout<<nam[a]<<endl;
						  
	  cout<<":: ADDRESS ::";
	  cout<<add[a]<<endl;
						  
	  cout<<":: CONTACT INFORMATION ::";
	  cout<<con[a]<<endl;
						  
	  cout<<":: COURSE ::";
	  cout<<cor[a]<<endl;
						  
	  cout<<":: YEAR LEVEL ::";
	  cout<<yel[a];
}

else
{
 cout<<"NO RECORDS FOUND"<<endl;
}
}
cout<<"\n\n\n\n\n************************************************"<<endl;
cout<<"***Please press any key to return to the Menu***"<<endl;
cout<<"************************************************"<<endl;

getch();
break;
}

case '3':
{
system("cls");
cout<<"Modify/edit the current student's info"<<endl;
cout<<"\n\nType student's ID NUMBER : ";
cin>>search;

for (a=0; a<num_rec ; a++)
{
	
if (search == idn[a])
{
 

	cout<<"Enter your Name:: ";
	getline (cin, nam[a]);
	cout<<"Enter your Address:: ";
	getline (cin, add[a]);
	cout<<"Enter your Contact Information:: ";
	getline (cin, con[a]);
	cout<<"Enter your Course:: ";
	getline (cin, cor[a]);
	cout<<"Enter your Year Level:: ";
	getline (cin, yel[a]);
	cout<<"\n\n\n You have Successfully modified the record"<<endl;
}

else
{
cout<<" RECORD NOT FOUND"<<endl;
}
}
cout<<"\n\n\n\n\n************************************************"<<endl;
cout<<"***Please press any key to return to the Menu***"<<endl;
cout<<"************************************************"<<endl;

getch();
break;
}

case '4':
{
cout<<"gettin' shit done.so wait nigga >:O press any button!";
getch();
break;
}
  }
}

while(myinput>=5);
return 0;
}



idk.. where im getting here while helping me though can you spot other mistakes i made here..it seems also that i can only add 1 record in my program
Topic archived. No new replies allowed.