input and output for array

I"m practicing writing code and learning how to use array. I'm able to input the data..but the data that gets spit from the array is wrong once I put in the number 100, or the total of all the values input = 100 or more..
example..if I put in 5, 10 15.. the output would be 5,10,15..so far so good.
but if i put in 5, 100, 10 15.. the output would be 5, and then garbage..
or if i input 85,15..the output would be 85..and then garbage..cause the total is over 100...I don't get why it cares about the total of the input..when its just storeing the values in a space.. it shouldnt be totaling the values..

The code is a little messy but its just practice.

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
  // vector::push_back
#include <iostream>
//#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
  // std::vector<int> myvector;
 int myint=0;
  int size=0;
  int count=0;
int total=0; 
float g=0;  
double view[1000];
int i=0;
int t=0;   

  cout << "Please enter a grade between 0 and 100):\n";

  while(myint != -1){
    cin >> view[myint];
    myint = view[myint];
    if(myint > 100 || myint < -1)
    { count = count + 1;
	cout << "Please enter a grade between 0 and 100):\n";
    }t = myint;
    //view[g] = t;
  //  myvector.push_back (myint);
    size = size + 1;
  } //while (myint !=-1);
  
	

//  std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
  
// size = (myvector.size()) -1;
  size = size - count;
 //cout <<"size is" << size << endl; 
 cout <<"size is" << size << endl;
  cout <<"count is" << count << endl;

	for(i = 0; i < size - 1; i++){
	  std::cout << "\t" "grade" << i << "\t" "is" "\t" << view[i]  << " numbers.\n";
	  //cout<<"\t"<<i<<"\t\t"<<n[i]<<"\n";
}

 system("PAUSE");
  return 0;
} 
1
2
3
4
5
 while(myint != -1){
    cin >> view[myint];
    myint = view[myint];
    //...
  


that whole section of code doesn't really make sense.

What are you trying to do here? between lines 22 - 32 So I can make an appropriate suggestions.
ok when I tried to use the sentinal to control the loop, for some reason it would not apply the value ton myint.. unless i assigned the value to myint after it is input ino the array..

if I left out myint = view[myint]; I can't use the -1 to get out of loop
Ok so like i said this is practice code. I use portion of that code in another program that worked out fine..but the key thing Im trying to figure out is why I wont print out the correct data after 100 as I said above.. the code in betwween 22-32 was part of an if statement to make sure the person only input grade value between 0 and 100 anything outside of that would get rejected..
Ok I think I get get what you mean but lets try doing it a different way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
int a = 0; 
int temp; 
while (a < 1000) { 
cout << "Please insert grade between 0 and 100: " << endl; 
cin >> temp; 
if (temp == -1)  { 
    break;   
}
else if  (temp < 0 || temp > 100) { 
    cout << "Please insert a number between 0 and 100: " << endl;
  
}
else {
  view [a] = temp;
  a++;  // a will double over as size in all truth. 
}  
}


This is what I believe you want or at least something similar to this. This is a possible replacement for lines 22-32 some of your other code will have change slightly.
Last edited on
Ok..I will try this and get back to you.
Ok that seems to work, however it seems I cant use float or double in the array. when i change temp to a float and the array to a float..I get an error, same when I change them both to double... any idea why?
can you post the code now?
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

// vector::push_back
#include <iostream>
//#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
  // std::vector<int> myvector;
 int myint=0;
  int size=0;
  int count=0;
int total=0; 
int g=0;  
int view[1000];
int i=0;
int t=0;   

int a = 0; 
int temp; 
while (a < 1000) { 
cout << "Please insert grade between 0 and 100: " << endl; 
cin >> temp; 
if (temp == -1)  { 
    break;   
}
else if  (temp < 0 || temp > 100) { 
    cout << "Please insert a number between 0 and 100: " << endl;
  
}
else {
  view [a] = temp;
  size = size + 1;
  a++;  // a will double over as size in all truth. 
}  
}


  /*cout << "Please enter a grade between 0 and 100):\n";

  while(myint != -1){
    cin >> view[myint];
    myint = view[myint];
    if(myint > 100 || myint < -1)
    { count = count + 1;
	cout << "Please enter a grade between 0 and 100):\n";
    }t = myint;
    //view[g] = t;
  //  myvector.push_back (myint);
    size = size + 1;
  } //while (myint !=-1);*/
  
	

//  std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
  
// size = (myvector.size()) -1;
  size = size - count;
 //cout <<"size is" << size << endl; 
 cout <<"size is" << size << endl;
  cout <<"count is" << count << endl;

	for(i = 0; i < size; i++){
	  std::cout << "\t" "grade" << i << "\t" "is" "\t" << view[i]  << " numbers.\n";
	  //cout<<"\t"<<i<<"\t\t"<<n[i]<<"\n";
}

 system("PAUSE");
  return 0;
} 
I got it to work and my out put was this:


Please insert grade between 0 and 100:
7.89
Please insert grade between 0 and 100:
92
Please insert grade between 0 and 100:
101
Please insert a number between 0 and 100:
Please insert grade between 0 and 100:
-2
Please insert a number between 0 and 100:
Please insert grade between 0 and 100:
56
Please insert grade between 0 and 100:
76.7
Please insert grade between 0 and 100:
-1
        grade0  is      7.89 numbers.
        grade1  is      92 numbers.
        grade2  is      56 numbers.
        grade3  is      76.7 numbers.
Press any key to continue . . .


However, keep in mind I altered your code just a bit to reflect how I would write and got rid of some unnecessary variables and comments. I'll post below:
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
 
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
  
double view[1000];
   

int a = 0; 
double temp; 
while (a < 1000) { 
cout << "Please insert grade between 0 and 100: " << endl; 
cin >> temp; 
if (temp == -1)  { 
    break;   
}
else if  (temp < 0 || temp > 100) { 
    cout << "Please insert a number between 0 and 100: " << endl;
  
}
else {
  view [a] = temp;
  a++;  
}  
}

	for(int i = 0; i < a; i++){
	 cout << "\t" "grade" << i << "\t" "is" "\t" << view[i]  << " numbers.\n";
}                                            //might want to switch to i+1.

  return 0;
}
Last edited on
Topic archived. No new replies allowed.