Thread: Question about a certain code with the C++ syntax

  1. #31
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    I will play along with that. Now a seperate issue for Jim.

    Code:
    for (index==0; index<numberofterms; index++)
    {   
        cout<<"Enter temperature you want to input in Celsius"<<(index+1);
        cout<<": ";
        cin>>temperature[index];
        temperature(numberofterms).push_back(temperature[index])
    
    Would this be an appropriate use of the push back function?

    Also another question is when I tried running my code, it does the first part and then it doesnt complete the rest of the code. It must be an error with the code correct? Even though the complier hasnt detected anything wrong.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, I suggest you look up what push_back is and how it's used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Even though the complier hasnt detected anything wrong.
    Just because the compiler doesn't detect an error doesn't mean your program is correct.
    Also another question is when I tried running my code, it does the first part and then it doesnt complete the rest of the code.
    Run your program through your debugger. Your debugger will tell you exactly where it detected the error and you will be able to examine your variables at the time of the crash. Then you should be able to determine what variable is incorrect and back trace until you find the actual problem.

    Jim

  4. #34
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Fixed the problem I think.

    //Running Mean for 3 points with even weights


    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <numeric>
    using namespace std;




    int main ()


    {
    float numberofterms;
    float number_of_elements_avg;
    vector<float> temperature; //Define a vector of 5 floats
    vector<float> movingavg;
    int index; //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 (index==0; index<numberofterms; index++)
    {
    float average;

    cout<<"Enter temperature you want to input in Celsius"<<(index+1);
    cout<<": ";
    cin>>temperature[index];
    temperature.push_back(temperature[index]);

    if (index==0)
    { movingavg[index]=temperature[index];
    cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
    continue;
    }
    else if (index==numberofterms-1)
    {
    movingavg[index]=temperature[index];
    cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
    continue;
    }
    for (int index =0; numberofterms-number_of_elements_avg; index++);
    {
    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);
    }
    //average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
    //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;


    }

    PROBLEM: Instainted from here. What kind of error is that?

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Add code tags, indent code, give the real (full) compile errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    Add code tags, indent code, give the real (full) compile errors.
    CODE ERROR:
    Line 46: C:\Users\.... Message: Instantiated from here. That's the line in the average part.

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's not the full error. I repeat the above:
    Add code tags, indent code, give the real (full) compile errors.
    Copy everything. All the output.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    That's not the full error. I repeat the above:
    Add code tags, indent code, give the real (full) compile errors.
    Copy everything. All the output.
    Let me attempt what you are trying to ask.

    My Code:
    Code:
    //Running Mean for 3 points with even weights
    
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <numeric>
    using namespace std;
    
    
    
    
    int main ()
    
    
    {
    float numberofterms;
    float number_of_elements_avg;  
    vector<float> temperature; //Define a vector of 5 floats
    vector<float> movingavg;
    int index; //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 (index==0; index<numberofterms; index++)
    {   
        float average;
        
        cout<<"Enter temperature you want to input in Celsius"<<(index+1);
        cout<<": ";
        cin>>temperature[index];
        temperature.push_back(temperature[index]);
         
         if (index==0)
         { movingavg[index]=temperature[index];
           cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
           continue;
         }
         else if (index==numberofterms-1)
         {
         movingavg[index]=temperature[index];
         cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
         continue;
         }
           for (int index =0; numberofterms-number_of_elements_avg; index++); 
             {
             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);
             }
            //average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
            //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;
    
    
    }



    Compiler Error Message from C++ Bloodshead:


    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp" -o "C:\Users\FutureNWS4caster\Desktop\C++ Revised.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
    C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp: In function `int main()':
    C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:41: warning: passing `float' for converting 1 of `typename _Alloc::reference std::vector<_Tp, _Alloc>:perator[](size_t) [with _Tp = float, _Alloc = std::allocator<float>]'


    C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:46: warning: name lookup of `index' changed


    C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:17: warning: matches this `index' under ISO standard rules


    C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:44: warning: matches this `index' under old rules
    C:\Users\FutureNWS4caster\Desktop\C++ Revised.cpp:46: warning: passing `float' for converting 1 of `__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>:perator-(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:46: warning: passing `float' for converting 1 of `__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>:perator+(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = float*, _Container = std::vector<float, std::allocator<float> >]'


    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:46: instantiated from here
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_numeric.h:89: warning: converting to `int' from `float'


    Execution terminated




  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I fixed indentation for your code:
    Code:
    //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;  
    	vector<float> temperature; //Define a vector of 5 floats
    	vector<float> movingavg;
    	int index; //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 (index==0; index<numberofterms; index++)
    	{   
    		float average;
    
    		cout<<"Enter temperature you want to input in Celsius"<<(index+1);
    		cout<<": ";
    		cin>>temperature[index];
    		temperature.push_back(temperature[index]);
    
    		if (index==0)
    		{
    			movingavg[index]=temperature[index];
    			cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
    			continue;
    		}
    		else if (index==numberofterms-1)
    		{
    			movingavg[index]=temperature[index];
    			cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
    			continue;
    		}
    		for (int index =0; numberofterms-number_of_elements_avg; index++); 
    		{
    			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);
    		}
    		//average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
    		//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;
    }
    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.

    Aside from all of that, you need to learn code flow. You don't understand the code or what it does. So take your time to understand it. Read up about what std::accumulate is, what it does and how to use it.
    Then re-work your code to make it work as it should, or at least make a flowchart describing what it should do, then make one what it currently does do.

    And while you're at it, get rid of bloodshed and get Code::Blocks or Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Not working for some reason. All the errors are gone but they are just two errors. Strange???

    Ok I download visual Studio C++. But somehow its not working even those the only errors per say I am getting is Warning.

    Code:
    //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 ["<<i<<"] = "<<average<<" C"<<endl;
            cout<<"The original temperature ["<< i+1<<"]"<<tempstorage<<"] C has been replaced." <<endl;
        }
     
        system ("PAUSE");
        return 0;
    }
    OUTPUT:

    1>------ Build started: Project: RunningMean, Configuration: Debug Win32 ------1> RunningMean.cpp
    1>c:\users\futurenws4caster\documents\visual studio 2010\projects\runningmean\runningmean\RunningMean. h(48): warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data
    1>c:\users\futurenws4caster\documents\visual studio 2010\projects\runningmean\runningmean\RunningMean. h(48): warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data
    1> .NETFramework,Version=v4.0.AssemblyAttributes.cpp
    1> RunningMean.vcxproj -> c:\users\futurenws4caster\documents\visual studio 2010\Projects\RunningMean\Debug\RunningMean.dll
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


  11. #41
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try using a floating point number for the third argument of the accumulate call. For zero try 0.0.

    Jim

  12. #42
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    Try using a floating point number for the third argument of the accumulate call. For zero try 0.0.

    Jim
    If I understood correctly

    Code:
    for (int j=0; j<numberofterms-number_of_elements_avg; j++)
            {
                average = accumulate(temperature.begin() + j - number_of_elements_avg/2.0, temperature.begin() + j + number_of_elements_avg/2.0 + 1.0, 0.0) / (number_of_elements_avg + 1.0);
            }
    Do I need to change to the bold? Not only the 0.0 as well?

  13. #43
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    Try using a floating point number for the third argument of the accumulate call. For zero try 0.0.

    Jim
    If I understood correctly

    Code:
    for (int j=0; j<numberofterms-number_of_elements_avg; j++)
            {
                average = accumulate(temperature.begin() + j - number_of_elements_avg/2.0, temperature.begin() + j + number_of_elements_avg/2.0 + 1.0, 0.0) / (number_of_elements_avg + 1.0);
            }
    Still showing this as a warning. This part of the code I think is the only problem.

    1>------ Build started: Project: RunningMean, Configuration: Debug Win32 ------
    1> RunningMean.cpp
    1>c:\users\futurenws4caster\documents\visual studio 2010\projects\runningmean\runningmean\RunningMean. h(48): warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data
    1>c:\users\futurenws4caster\documents\visual studio 2010\projects\runningmean\runningmean\RunningMean. h(48): warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data
    1>c:\users\futurenws4caster\documents\visual studio 2010\projects\runningmean\runningmean\RunningMean. h(48): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    1> RunningMean.vcxproj -> c:\users\futurenws4caster\documents\visual studio 2010\Projects\RunningMean\Debug\RunningMean.dll
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    Do I need to change to the bold? Not only the 0.0 as well?

  14. #44
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No you only need to change the last parameter. The other items must be integers because they are being used as indexes. I would also recommend you do all the calculations separately and then just plug in a variable into your accumulate function. For example instead of using this calculation inside the accumulation function I would assign it to a variable.
    Code:
    // j - number_of_elements_avg/2.0 
    unsigned int start = j - number_of_elements_avg/2;
    average = accumulate(temperature.begin() + start, t ......
    You should also check and insure that j is larger than the negative calculation. Remember start can't be negative, and it shouldn't be larger than your vector size.

    Jim

  15. #45
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    No you only need to change the last parameter. The other items must be integers because they are being used as indexes. I would also recommend you do all the calculations separately and then just plug in a variable into your accumulate function. For example instead of using this calculation inside the accumulation function I would assign it to a variable.
    Code:
    // j - number_of_elements_avg/2.0 
    unsigned int start = j - number_of_elements_avg/2;
    average = accumulate(temperature.begin() + start, t ......
    You should also check and insure that j is larger than the negative calculation. Remember start can't be negative, and it shouldn't be larger than your vector size.

    Jim
    I see break up the parts to find the source of the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 12-09-2011, 03:28 PM
  2. Syntax Adjustment in C code block
    By Dr Diablo in forum C Programming
    Replies: 5
    Last Post: 11-26-2008, 05:16 AM
  3. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 01:16 AM
  4. code syntax help
    By 182 in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2006, 12:57 PM
  5. What is the syntax for a #define for several lines of code?
    By Jonas_Valleskog in forum C Programming
    Replies: 7
    Last Post: 01-31-2003, 12:22 PM