Character Array and cstring library

Apr 10, 2010 at 8:58pm
After i prompt the user for input character using array, my program fails to
check for bad data and i get stuck since i'm not allowed to use stdio library.
How would you check the input for char? can i use const char myArray[][]?
Apr 10, 2010 at 9:12pm
Can you post some code?

i'm not allowed to use stdio library
Why?
How are you reading input?
Apr 11, 2010 at 3:20am
Thank you for your reply. My program works fine but the check for the input name has a bug. I've having issue with how to use character array (c-string) and check for character only.


#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstring>

using namespace std;

const char MAX_CHAR = 101;

int main ()
{
char item[MAX_CHAR];
char response=' ';
float totalCost = 0.0;
float itemCost = 0.0;
char myArray[10];
p= &item[i];
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);

do
{
cout<<"\nPlease Enter a name: ";
cin.get(item, MAX_CHAR, '\n');
while(!cin)
{
cin.clear();
cin.ignore(100, '\n');
cout<<endl<<"Invalid input! Please try again: ";
cin.get(item, MAX_CHAR, '\n');

for (int i=0; i<=100; i++)
{
p = &item[i];
if (p[i]==0 || p[i]<=10)
{
cout<<"Enter the name again:";
}
}
}
cin.ignore(100, '\n');

cout<<"\nPlease Enter the price: (e.g. 2.99) ";
cin >>itemCost;
while(!cin)
{
cin.clear();
cin.ignore(100,'\n');
cout<<endl<<"Invalid input! Please try again with price > 0: ";
cin>>itemCost;
}
cin.ignore(100, '\n');
totalCost = totalCost + itemCost;
cout << endl << item << ": " << itemCost <<endl;
cout << endl<< "\nYour Total Balance is: " <<totalCost;
cout.setf(ios::fixed, ios::floatfield );
cout.setf(ios::showpoint);
cout.precision(2);
cout <<endl<< "\nWould you like to continue (y/n): ";
cin >>response;
cin.ignore(100, '\n');
cout<<endl;
response = tolower(response);
}while(response =='y' || response =='Y');
//the following if statement does not work for some reason. i do not know how to get it fixed
//if (response =='n' || response =='N')
//{
cout<<"Thank you for using the program!"<<endl;
//}

return 0;
}
Last edited on Apr 12, 2010 at 5:43am
Apr 11, 2010 at 10:35am
Please edit your previous post to include [code][/code] tags ( http://www.cplusplus.com/articles/firedraco1/ )
Is your problem regarding these lines?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout<<"\nPlease Enter a name: ";
cin.get(item, MAX_CHAR, '\n');
while(!cin)
{
 cin.clear();
 cin.ignore(100, '\n');
 cout<<endl<<"Invalid input! Please try again: ";
 cin.get(item, MAX_CHAR, '\n');

 for (int i=0; i<=255; i++)
 {
  p = &item[i];
  if (p[i]==0 || p[i]<=10)
  {
   cout<<"Enter the name again:";
  }
 }
}
Last edited on Apr 11, 2010 at 10:57am
Apr 11, 2010 at 6:17pm
Yes please, this is where i have problem (make sure that user input only characters for name.
Sorry, this is my first time posting a code and i did not know i should include tags. Please, my assignment is due by 11:50pm tonight and i tried for hours yesterday:( but can't make it to work. Thank you

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstring>

using namespace std;

const char MAX_CHAR = 101;

int main ()
{
char item[MAX_CHAR]; 
char response=' ';
float totalCost = 0.0;
float itemCost = 0.0;
char myArray[10];

cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);

do 
{ 
cout<<"\nPlease Enter a name: ";
cin.get(item, MAX_CHAR, '\n');
while(!cin) 
{
cin.clear();
cin.ignore(100, '\n');
cout<<endl<<"Invalid input! Please try again: ";
cin.get(item, MAX_CHAR, '\n');

for (int i=0; i<=255; i++)
{
p = &item[i];
if (p[i]==0 || p[i]<=10)
{
cout<<"Enter the name again:";
}
}
}
cin.ignore(100, '\n');

cout<<"\nPlease Enter the price: (e.g. 2.99) ";
cin >>itemCost;
while(!cin) 
{
cin.clear();
cin.ignore(100,'\n');
cout<<endl<<"Invalid input! Please try again with price > 0: ";
cin>>itemCost;
}
cin.ignore(100, '\n');
totalCost = totalCost + itemCost;
cout << endl << item << ": " << itemCost <<endl;
cout << endl<< "\nYour Total Balance is: " <<totalCost;
cout.setf(ios::fixed, ios::floatfield );
cout.setf(ios::showpoint);
cout.precision(2);
cout <<endl<< "\nWould you like to continue (y/n): ";
cin >>response;
cin.ignore(100, '\n');
cout<<endl;
response = tolower(response);
}while(response =='y' || response =='Y'); 
//the following if statement does not work for some reason. i do not know how to get it fixed
//if (response =='n' || response =='N')
//{
cout<<"Thank you for using the program!"<<endl; 
//}

return 0;
}


Apr 11, 2010 at 6:26pm
where does 'p' ( lines 36-37 ) comes from?
In loop at line 34 you are going from 0 to 255 but item has a size of 101, this will cause overflow on line 36
And I don't understand what that for is supposed to do anyway
Apr 11, 2010 at 7:57pm
p is a character pointer. I've fixed the overflow. Thabk you. The reason I'm using the for loop to read the input string and do the check for characters only. How would you do it without arrays?

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstring>

using namespace std;

const char MAX_CHAR = 101;

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
int main ()
{
char item[MAX_CHAR]; 
char response=' ';
float totalCost = 0.0;
float itemCost = 0.0;
char myArray[10];
char* p;

cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);

do 
{ 
cout<<"\nPlease Enter a name: ";
cin.get(item, MAX_CHAR, '\n');
while(!cin) 
{
cin.clear();
cin.ignore(100, '\n');
cout<<endl<<"Invalid input! Please try again: ";
cin.get(item, MAX_CHAR, '\n');

for (int i=0; i<=100; i++)
{
p = &item[i];
if (p[i]==0 || p[i]<=10)
{
cout<<"Enter the name again:";
}
}
}
cin.ignore(100, '\n');

cout<<"\nPlease Enter the price: (e.g. 2.99) ";
cin >>itemCost;
while(!cin) 
{
cin.clear();
cin.ignore(100,'\n');
cout<<endl<<"Invalid input! Please try again with price > 0: ";
cin>>itemCost;
}
cin.ignore(100, '\n');
totalCost = totalCost + itemCost;
cout << endl << item << ": " << itemCost <<endl;
cout << endl<< "\nYour Total Balance is: " <<totalCost;
cout.setf(ios::fixed, ios::floatfield );
cout.setf(ios::showpoint);
cout.precision(2);
cout <<endl<< "\nWould you like to continue (y/n): ";
cin >>response;
cin.ignore(100, '\n');
cout<<endl;
response = tolower(response);
}while(response =='y' || response =='Y'); 
cout <<"Thank for using the program!"<<endl;
return 0;
}
Topic archived. No new replies allowed.