C++ Programming assistance of the errors

Hey Guys,

I am wondering if you guys can help me fix some of the errors I am getting. I want to do a running average for example like three points.

Here's my 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

//Running Mean for 3 points with even weights
 
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <numeric>
using namespace std;
 
int main ()
{
    float numberofterms;
    float number_of_elements_avg; 
    flot tempstorage;
    vector<float> temperature; //Define a vector of 5 floats
    vector<float> movingavg;
    int i; //Loop counter
 
    cout<<"Enter the number of terms you want: " <<endl;
    cin>>numberofterms;
    cout<<"Enter how many pt average you would like to take: "<<endl;
    cin>>number_of_elements_avg;
 
 
    for (i=0; index<numberofterms; i++)
    {  
        float average;
 
        cout<<"Enter temperature you want to input in Celsius"<<(index+1);
        cout<<": ";
        cin>>tempstorage;
        temperature.push_back(tempstorage);
 
        if (i=0)
        {
            movingavg[index]=temperature[index];
            cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
            continue;
        }
        else if (i=numberofterms-1)
        {
            movingavg[index]=temperature[index];
            cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
            continue;
        }
        for (int j=1; j<numberofterms; numberofterms-number_of_elements_avg; j++);
        {
            average = accumulate(temperature.begin() + index - number_of_elements_avg/2, temperature.begin() + index + number_of_elements_avg/2 + 1, 0) / (number_of_elements_avg + 1);
        }
        //NOT PART OF THE CODE average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
        //NOT PART OF THE CODE movingavg[index]=average;
        cout<<"Temperature ["<<index+1<<"] = "<<average<<" C"<<endl;
        cout<<"The original temperature ["<< index+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;
    }
 
    system ("PAUSE");
    return 0;
}



Errors that I am getting that I may have resolved:

The errors just wouldn't line up with the line numbers in the posted code, so here are the warnings I got when compiling:
Warning 1 warning C4553: '==' : operator has no effect; did you intend '='? 24
Warning 2 warning C4244: 'argument' : conversion from 'float' to 'unsigned int', possible loss of data 42
Warning 3 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 47
Warning 4 warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data 47
Warning 5 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 47
Warning 6 warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data 47
Warning 7 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 51
Warning 8 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 52
Warning 9 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 52
Warning 10 warning C6246: Local declaration of 'index' hides declaration of the same name in outer scope. For additional information, see previous declaration at line '16' of '': Lines: 16 45
Warning 11 warning C6001: Using uninitialized memory 'index': Lines: 12, 13, 14, 15, 16, 18, 19, 20, 21, 24 24
Warning 12 warning C4700: uninitialized local variable 'index' used 24

First warnings says you messed up the loop initialization.
Second warnings says you are using a float as index to your vector. An index can only be a whole number. Element 3.5 does not exist, after all.
Warnings 3, 5, 7-9 comes from your messed up index variable and because you use the same index variable in both loops.
Warning 4 and 6 comes from your line calculating the average. You can only have a whole number of elements you want to average. You can't average 3.5 elements.
Warning 10 says you are re-using the same variable name for the second loop as for the first.
Warning 11 and 12 says you are using index but never initialized it.

Can you guys help me? Thanks.
Looks like you answered your own question.
Nimmy, what exactly do you want us to do? You kinda explained what's happening anyway, so can't you fix the errors yourself?
Well here's what I did so far and I am still getting these errors. Sorry about that. I know at the end it needs to be tempstorage.

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
//Running Mean for 3 points with even weights
 
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <numeric>
using namespace std;
 
int main ()
{
    int numberofterms;
    float number_of_elements_avg; 
    float tempstorage;
    vector<float> temperature; //Define a vector of 5 floats
    vector<float> movingavg;
    int i; //Loop counter
 
    cout<<"Enter the number of terms you want: " <<endl;
    cin>>numberofterms;
    cout<<"Enter how many pt average you would like to take: "<<endl;
    cin>>number_of_elements_avg;
 
 
    for (i=0; i<numberofterms; i++)
    {  
        float average;
 
        cout<<"Enter temperature you want to input in Celsius"<<(i+1);
        cout<<": ";
        cin>>tempstorage;
        temperature.push_back(tempstorage);
 
        if (i=0)
        {
            movingavg[i]=temperature[i];
            cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
            continue;
        }
        else if (i=numberofterms-1)
        {
            movingavg[i]=temperature[i];
            cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
            continue;
        }
        for (int j=0; j<numberofterms-number_of_elements_avg; j++)
        {
            average = accumulate(temperature.begin() + j - number_of_elements_avg/2, temperature.begin() + j + number_of_elements_avg/2 + 1, 0) / (number_of_elements_avg + 1);
        }
        //NOT PART OF THE CODE average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
        //NOT PART OF THE CODE movingavg[index]=average;
        cout<<"Temperature ["<<j<<"] = "<<average<<" C"<<endl;
        cout<<"The original temperature ["<< j+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;
    }
 
    system ("PAUSE");
    return 0;
}



C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:3:20: stdafx.h: No such file or directory
C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp: In function `int main()':
C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:48: warning: passing `float' for converting 1 of `__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = float*, _Container = std::vector<float, std::allocator<float> >]'

C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:48: warning: passing `float' for converting 1 of `__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = float*, _Container = std::vector<float, std::allocator<float> >]'
C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:52: error: name lookup of `j' changed for new ISO `for' scoping
C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:46: error:   using obsolete binding at `j'
C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:53: error: `index' undeclared (first use this function)

C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:53: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_numeric.h: In function `_Tp std::accumulate(_InputIterator, _InputIterator, _Tp) [with _InputIterator = __gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > >, _Tp = int]':
C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:48:   instantiated from here
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_numeric.h:89: warning: converting to `int' from `float'

Execution terminated


Using Dev C++ so dont know what the error is.
Last edited on
I figured out the problem but now trying to figure out why my executable is crashing. It might be because there vector movingavg has no elements?


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


//Running Mean for 3 points with even weights
 
#include <iostream>
#include <iomanip>
#include <vector>
#include <numeric>
#include <functional>
using namespace std;
 
int main ()
{
    int numberofterms;
    int number_of_elements_avg; 
    double tempstorage;
    vector<double> temperature; //Define a vector of 5 floats
    vector<double> movingavg;
    int i; //Loop counter
 
    cout<<"Enter the number of terms you want: " <<endl;
    cin>>numberofterms;
    cout<<"Enter how many pt average you would like to take: "<<endl;
    cin>>number_of_elements_avg;
 
 
    for (i=0; i<numberofterms; i++)
    {  
        double average;
 
        cout<<"Enter temperature you want to input in Celsius "<<(i+1);
        cout<<": ";
        cin>>tempstorage;
        temperature.push_back(tempstorage);
 
        if (i=0)
        {
            movingavg[0]=temperature[0];
            cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
            continue;
        }
        else if (i=numberofterms-1)
        {
            movingavg[numberofterms-1]=temperature[numberofterms-1];
            cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
            continue;
        }
        for (int j=0; j<numberofterms-number_of_elements_avg; j++)
        {
            // j - number_of_elements_avg/2.0 
            unsigned int start = j - number_of_elements_avg/2;
            average = accumulate(temperature.begin() + start, temperature.begin() + j + number_of_elements_avg/2 + 1, 0.0) / (number_of_elements_avg + 1);
        }
        //NOT PART OF THE CODE average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
        //NOT PART OF THE CODE movingavg[index]=average;
        cout<<"Temperature ["<<i<<"] = "<<average<<" C"<<endl;
        cout<<"The original temperature ["<< i+1<<"]"<<tempstorage<<"] C has been replaced." <<endl;
    }
 
    system ("PAUSE");
    return 0;
}


OUTPUT:


Now that I got the code running. Here's what the executable is displaying. The bold is the input.

Enter the number of terms you want:
4
Enter how many point average you want take:
3
Enter Temp input in C 1: 20

AND THEN IT CRASHES AT THAT POINT. Why isnt it going through the for loop?
Last edited on
Topic archived. No new replies allowed.