increasing integers automatically within a two dimensional array

i am new to programming all together but i have been writing a program in c++ and im coming up against an issue with my array.

#include<iostream>
#include<iomanip>
#include<string>

using namespace

int main ()
{
int a ;
int b ;
char answer ('Y') ;

// begin array, table of scores

int grade [3][5] = {0} ;


cout << "\n \t*******************************************************" << endl ;

cout << setw(5) << "\n \t\t grade" ;
cout << setw(8) << "A" ;
cout << setw(5) << "B" ;
cout << setw(5) << "C" ;
cout << setw(5) << "D" ;
cout << setw(8) << "Fail" << endl ;

cout << setw(2) << "\n \t\t Year One" ;
cout << setw(5) << grade [0][0] ; // year one A
cout << setw(5) << grade [0][1] ; // year one B
cout << setw(5) << grade [0][2] ; // Year one c
cout << setw(5) << grade [0][3] ; // year one D
cout << setw(7) << grade [0][4] << endl ; // year one fail

cout << setw(2) << "\n \t\t Year Two" ;
cout << setw(5) << grade [1][0] ;
cout << setw(5) << grade [1][1] ;
cout << setw(5) << grade [1][2] ;
cout << setw(5) << grade [1][3] ;
cout << setw(7) << grade [1][4] << endl ;

cout << setw(2) << "\n \t\t Year Three" ;
cout << setw(3) << grade [2][0] ;
cout << setw(5) << grade [2][1] ;
cout << setw(5) << grade [2][2] ;
cout << setw(5) << grade [2][3] ;
cout << setw(7) << grade [2][4] << endl ;

cout << "\n \t*******************************************************" << endl ;



do{

cout << "\n\n \t\t please enter the students year :" ;
cin >> b ;

if (b == 1)
{cout << "\n \t\t Year One" << endl ;
cin.get();}

if (b == 2)
{cout << "\n \t\t Year Two" << endl ;
cin.get ();}

if (b == 3)
{cout << "\n \t\t Year Three" << endl ;
cin.get();}

if(b != 1 && b != 2 && b != 3)
{cout << "\n \t\t Please enter valid year 1,2 or 3 :" ;
cin >> b ;
cin >> answer ;
cin.get(); }


cout << "\n\n \t\t please enter the students score :" ;
cin >> a ;

if(a <= 29 )
{
cout << "\n \t\t The sudent has failed" << endl ;
cin.get();
}
if ( a >= 30 && a <= 44 )
{
cout << "\n \t\t The Students Grade is D" << endl ;
cin.get();
}
if ( a >= 45 && a <= 55 )
{
cout << "\n \t\t The Students Grade is C" << endl ;
cin.get () ;
}
if (a >= 56 && a <= 69)
{
cout << "\n \t\t The Students Grade is B" << endl ;
cin.get () ;
}
if (a >= 70 && a <= 100)
{
cout << "\n \t\t The Students Grade is A" << endl ;
cin.get () ;
}
if (a >= 101)
{
cout << " \n \t\t Please enter a Valid score between 1 - 100 :" ;
cin >> a ;
cin.get () ;
}


cout << "\n\n\ \t\t would you like to continue :" ;
cin >> answer ;

} while(answer == 'Y' || answer == 'y') ;




cin.get () ;
return 0 ;
}

i am trying to get the program to increase say year one by 1 when the condition is met i have tried

if ( a >= 70 && a <= 100 && b == 1)
{grade [0][0] = 0 + 1;}

and

for (grade[0][0] = 0 ; a >= 70 && a <= 100 && b == 1 ; grade [0][0]++)
{grade [0][0]= 0 + 1 ;}

now all that i want is that the array will take the information from int a and int b and then add 1 to the appropriate part of the array

i have tried putting it in deferent places but its not working for ether. the program will run but it will not add to the array
I don't understand why I keep seeing this kind of thinking (please, no offense).

It's like you make yourself a peanut butter sandwich, eat it, then go back and try to put jam on it, then wonder why the sandwich wasn't a PB&J.

(1) You create the array, filling it with zeros
(2) You print the array
(3) You dink with the array
(4) You wonder why the printout of the array doesn't show changes

You have to make changes before you print. Use functions. Stick the stuff that prints in a function.

Feel free to use global variables for this -- you are lost enough already without the extra grief.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int grade [3][5] = {{0}};

void print()
{
  // stuff that prints the array goes here
}

int main()
{
   // stuff that modifies the array goes here

  print();
  cin.get();
  return 0;
}

BTW, please use [code] tags.
thanks for the answer, as i siad new, very new so not got all the rules down yet. its like having to play a grand master at chess but no one will tell me how the pieces move

again thanks for taking the time to answer
Is there a more defined explanation available. It's still not working.
The trick with programming is thinking things through. The rules are actually very broad -- that is, there is (almost always) no "one true way" to do things. The toughest part is dealing with syntax -- how things are written.

If you were told to describe how to make a PBJ, you might give a very simple explanation. Person trying to make one will come back and say "it's not working".

What exactly is not working?

A lot of your code is trying to do things out of order. It is like coloring, for example, a rainbow. Red on top, then orange, then yellow, the green, then blue, then magenta.

You've got most of the colors on the page, but they aren't in order.

For example, you ask the user for a year and get one with cin >> b;.
Then you tell the user what year he entered.
Then you check to see if the user entered a valid year, and if he hasn't ask him to try again.

Those steps are in the wrong order.

If you haven't already, I really recommend you use some functions to do the repetitive stuff. Getting a student's grade and year are a good thing to put in a function, since you will want to do it over and over and over again.

Also, you should use more descriptive variable names than "a" and "b". For example, a great name for a variable holding a year would be year. Makes sense, right?

Take a look through the tutorials on this site, and try to fix your program some. If you are still lost, post back with your current code and we'll see how we can help you best.
http://www.cplusplus.com/doc/tutorial/

Take heart. Learning this stuff is not easy. It just takes time and a lot of patience.
right thanks to your advice i have fixed a few issues i was having , the only thing i cant get to work is that i need the array to update dynamically in runtime. once i have entered int a, and int b , i want the program to take that information and plus one to the right part of the array. i have put a sample of an if statement i thought would work under void modify, but its not doing it
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
 #include<iostream>
    #include<iomanip>
    
    using namespace std ;
    
    int a ; 
    int b ;
    int grade [3][5] = {0} ;
    void modify  () ;
    void array   () ;
    void student () ;
    
    int main () 
    {   
        
        
        array    () ;
        modify   () ; 
        student  () ;
        
        
        
      
    cin.get () ;
    return  0  ;    
    }
    
    void modify ()
    {  
         if( a >= 70 && a <= 100 && b == 1)
           {grade [0][0]++ ; } 
         if (a >= 56 && a <= 69 && b == 1)
            {grade [0][1]++; }  
         
    }
    
    void array ()
    {
     int grade [3][5] = {0} ;
     
     cout << "\n \t*******************************************************" << endl ;
     
     cout << setw(5) << "\n \t\t grade" ;
     cout << setw(8) << "A" ;
     cout << setw(5) << "B" ;
     cout << setw(5) << "C" ;
     cout << setw(5) << "D" ;
     cout << setw(8) << "Fail" << endl ;
     
     cout << setw(2) << "\n \t\t Year one" ;
     cout << setw(5) << grade [0][0] ;
     cout << setw(5) << grade [0][1] ;
     cout << setw(5) << grade [0][2] ;
     cout << setw(5) << grade [0][3] ;
     cout << setw(7) << grade [0][4] << endl ;
     
     cout << setw(2) << "\n \t\t Year two" ;
     cout << setw(5) << grade [1][0] ;
     cout << setw(5) << grade [1][1] ;
     cout << setw(5) << grade [1][2] ;
     cout << setw(5) << grade [1][3] ;
     cout << setw(7) << grade [1][4] << endl ;
    
     cout << setw(2) << "\n \t\t Year two" ;
     cout << setw(5) << grade [2][0] ;
     cout << setw(5) << grade [2][1] ;
     cout << setw(5) << grade [2][2] ;
     cout << setw(5) << grade [2][3] ;
     cout << setw(7) << grade [2][4] << endl ;
     
     cout << "\n \t*******************************************************" << endl ;
     
     
     
     }  
        
    
     void student ()  
     {
        int a ;
        int b ; 
        char answer ('Y') ;
        
        do{  
           //system ("cls") ;
          
           cout << "\n\n\n \t\t please enter the students year :" ;
           cin >> b ; 
           
           if(b != 1 && b != 2 && b != 3)
           {cout << "\n \t\t Please enter valid year 1, 2 or 3 :" ;
           cin >> b ;} 
           
           if (b == 1)
           {cout << "\n \t\t Year One" << endl ;
           cin.get();}
           
           if (b == 2)
           {cout << "\n \t\t Year Two" << endl ;
           cin.get ();}
           
           if (b == 3)
           {cout << "\n \t\t Year Three" << endl ;
           cin.get();}
           
           cout << "\n\n\n \t\t please enter the students score :"  ;
           cin >> a ;
           
           if (a >= 100)
             {
                 cout << " \n\n\n \t\t Please enter a Valid score between 1 - 100 :" ;
                 cin >> a ;
                 cin.get () ; 
             }
       if(a <= 29 )
             { 
                 cout << "\n\n\n \t\t The sudent has failed" << endl ;
                 cin.get();
             }
       if ( a >= 30 && a <= 44 )
             {
                 cout << "\n\n\n \t\t The Students Grade is D" << endl ;
                 cin.get();
             }
       if ( a >= 45 && a <= 55 )
             {
                 cout << "\n\n\n \t\t The Students Grade is C" << endl ;
                 cin.get () ; 
             }
       if (a >= 56 && a <= 69)
             { 
                 cout << "\n\n\n \t\t The Students Grade is B" << endl ;
                 cin.get () ;
             }
       if (a >= 70 && a <= 100)
             {
                 cout << " \n\n\n \t\t The Students Grade is A" << endl ;
                 cin.get () ;
             }  
                   
             cout << "\n\n\n\ \t\t would you like to continue :" ;
             cin >> answer ;
             } while(answer == 'Y' || answer == 'y') ;
       
       
          
          
     }      
    

i have also tried putting the modifiers within the main function, within the array function at the top and bottom, and within the student function at the top and bottom, i have read a little about pointers and am starting to wonder if they are what i need
i think that you should use loops in void array function like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
     int grade [3][5] = {0} ;
     
     cout << "\n \t*******************************************************" << endl ;
     cout << setw(5) << "\n \t\t grade" ;
     cout << setw(8) << "A" ;
     cout << setw(5) << "B" ;
     cout << setw(5) << "C" ;
     cout << setw(5) << "D" ;
     cout << setw(8) << "Fail" << endl ;
	
     char namey[][]={"one","two","three"};

     for(int i=0;i<2;i++)
     {
	cout << setw(2) << "\n \t\t Year "<<namey[i];
 	cout << setw(5) << grade [i][0] ;
 	cout << setw(5) << grade [i][1] ;
 	cout << setw(5) << grade [i][2] ;
 	cout << setw(5) << grade [i][3] ;
 	cout << setw(7) << grade [i][4] << endl ;
     }
     cout << "\n \t*******************************************************" << endl ;
and too from line 90 to 104 you can use switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
switch(b)
{
	case 1:{//equivalent to if(b==1)
		cout << "\n \t\t Year One" << endl ;
      cin.get();
		break;
	}
	case 2:{//equivalent to if(b==2)
		cout << "\n \t\t Year Two" << endl ;
      cin.get ();
		break;
	}
	case 3:{//equivalent to if(b==3)
		cout << "\n \t\t Year Three" << endl ;
      cin.get();
		break;
	}
	default :{//equivalent to if(b != 1 && b != 2 && b != 3) or if no case matches then this one matches itself
		cout << "\n \t\t Please enter valid year 1, 2 or 3 :" ;
      cin >> b ;
	}
}
well i some days back did this
1
2
3
4
5
6
7
8
9
.
.
.
.
int foo(int size)
{
     int arr[size];
     return size/4;  //in my pc int datatype is 4 bytes big
}

then i used this function in main then passed different values
and for different values i got different returns(or the value foo() is returning)!!!
i think you can use this concept to make a variable array
thanks for your input, i was considering switching to a switch any way, the major problem is the increments in the array not sure why
1
2
 if (a >= 70 && a <= 100 && b == 1)
            {grade [0][0]++; }


as long as it has the variables and the location it should increase by one
please explain the problem properly(a little brief) (i am no too good in englsh)
if you look at the code i posted above the idea is that , the user can input a value for int a, then a second value for int b. with the if statements i have i can then create a switch statement that will be able to use the code i posted above to say , that part of the array should increase by one
I have fixed the issue thanks for the help.
Topic archived. No new replies allowed.