Hello, I have 1 error but I don't know what is it??

Write ONE C++ program that displays a menu with 4 choices and executes those choices for the end-user.
Your menu should include an informative header/description at the top of the Windows 32 console followed by the following menu text (written in bold here):
Choose one of the following, or choose to quit this program.

1. Display ASCII characters with codes from 32 to 256
2. Show two patterns using the '+' character
3. Read random numbers from a file and perform calculations on them
4. Quit

_________
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>


using namespace std;

int main()
{
 int option = 0;

 while (option!=4)
 {
 cout << endl;
 cout<<"choose one of the following options." << endl;
 cout<<"1. Print out ASCII characters." << endl;
 cout<<"2. Print out patterns." << endl;
 cout<<"3. Read random.txt." << endl;
 cout<<"4. Quit." << endl;
 cin >> option;
 if (option==1)
 {
 for(int i=1; i<=255-32; i++)
   {
    cout << (char)(i+32) << " " ;
 if ((i % 16) == 0)
     {
     cout << endl;
     }
   }
 }
 else if (option==2)
 {
 for(int i=0; i<10; i++)
 {
 for(int j=0; j<=i; j++)
 {
 cout << "+";
 }
 cout << endl;
 }
  for(int i=10; i>0; i--)
 {
  for(int j=0; j<i; j++)
  {
  cout << "+";
  }
  cout << endl;
  }
 }
 else if (option==3)
 {
 int total = 0;
 int size = 0;
 ifstream ifs( "E:\\random.txt" );
 string s;

 while( getline( ifs, s ) )
 {
 if ( s.length()>0 )
  {
  int v = atoi(s.c_str());
  size++;
  total = total + v;
  }
 }
 cout << "file has " << size << " numbers" << endl;
 cout << "the total is " << total << endl;
 cout << "the average is " << (total/size) << endl;
 }
 else if (option==4)
   {
   cout<<"quit the program"<<endl;
   }
 }
 return 0;
}
Your code compiles just fine, and it works perfectly UNTIL you get to option 3. If the file doesn't exist, the program will throw EXC_ARITHMETIC (divide by zero causes this).

And you don't want to use string::c_str() as an input to atoi(). Try searching for digits?
http://cplusplus.com/reference/string/string/find/

-Albatross
Hi nano,
I am a beginner, so don't know how fine is my solution. But let me try. When I checked the code it seems like when it was not getting any input from random.txt, it was giving error because behavior of division by 0 is undefined in c++ and that is what code is trying to do.

I modified code a bit by throwing domain_error exception and it is working fine.

Here is the modified 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

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<stdexcept>


using namespace std;

int main()
{
 int option = 0;

 while (option!=4)
 {
 cout << endl;
 cout<<"choose one of the following options." << endl;
 cout<<"1. Print out ASCII characters." << endl;
 cout<<"2. Print out patterns." << endl;
 cout<<"3. Read random.txt." << endl;
 cout<<"4. Quit." << endl;
 cin >> option;
 if (option==1)
 {
 for(int i=1; i<=255-32; i++)
   {
    cout << (char)(i+32) << " " ;
 if ((i % 16) == 0)
     {
     cout << endl;
     }
   }
 }
 else if (option==2)
 {
 for(int i=0; i<10; i++)
 {
 for(int j=0; j<=i; j++)
 {
 cout << "+";
 }
 cout << endl;
 }
  for(int i=10; i>0; i--)
 {
  for(int j=0; j<i; j++)
  {
  cout << "+";
  }
  cout << endl;
  }
 }
 else if (option==3)
 {
 int total = 0;
 int size = 0;
 ifstream ifs( "E:\\random.txt" );
 string s;

 while( getline( ifs, s ) )
 {
 if ( s.length()>0 )
  {
  int v = atoi(s.c_str());
  size++;
  total = total + v;
  }
 }
 try {
          if ( s.length()==0 )
  throw domain_error ("no valid input to read in random.txt");
  cout << "file has " << size << " numbers" << endl;
 cout << "the total is " << total << endl;
 cout << "the average is " << (total/size) << endl;

  }catch (domain_error err)
  {
      cout <<err.what();
        }


 }
 else if (option==4)
   {
   cout<<"quit the program"<<endl;
   }
 }
 return 0;
}



Hope it will work.

Regards
Last edited on
Um... we absolutely HATE giving out full or even partial code solutions on this forum. Don't do it if you want to keep your fingers. ;)

Oh, and by the way, that solution is way overblown.
http://cplusplus.com/reference/iostream/ifstream/is_open/

-Albatross
@Albatross
Sorry man, I am new to c++ don't have very good knowledge as I have already described. Just trying to solve with my tiny knowledge.

And code I am just copy pasting, no hard work. But I will keep in account your advice

Thanx
Topic archived. No new replies allowed.