CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Mar 2013
    Posts
    6

    Odd error from VS 2010 C++

    1>------ Build started: Project: Lab 5, Configuration: Debug Win32 ------
    1> data.txt
    1>c:\users\anthony\documents\visual studio 2010\projects\lab 5\lab 5\data.txt(1): error C2059: syntax error : 'constant'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    Here is the code. The program is supposed to input data from a file, do a couple simple calculations, then output it to another .txt file. Here is the code. This is my first post and am starting to get into computer programming so figured I should join a forum such as this one. Thanks for any help guys!!

    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std;

    void instruction();
    void openfiles (ifstream& in, ofstream& out);
    void process (double& sum, int n, int first, double& average,
    double& uncertainty, double currentvalue,
    ifstream& in, double max, double min, int mincount,
    int maxcount);
    int main()
    {
    ifstream in;
    ofstream out;
    double sum=0, average, max, min, uncertainty, currentvalue;
    int n=1, first, maxcount, mincount;
    in >> first;
    in >> currentvalue;
    min = currentvalue;
    max = currentvalue;
    maxcount = n;
    mincount = n;



    instruction ();
    openfiles (in, out);
    process (sum, n, first, average, uncertainty, currentvalue, in, max, min,
    mincount, maxcount);

    return 0;
    }



    //Functions---

    void instruction()

    {
    cout << "***Physics 4AL Data Calculation From Data File.***\n";


    }

    void openfiles (ifstream& in, ofstream& out)
    {
    in.open("data.txt");

    if ( in.fail() )
    { cout <<"\nInput file failed to open.\n";
    }
    else {
    cout << "\nData file opened succesfully.\n";}

    out.open("dataout.txt");

    if ( out.fail() )
    { cout <<"\nOutput file failed to open.\n";
    }
    else {
    cout << "\nOutput file opened succesfully.\n\n";}




    }

    void process (double& sum, int n, int first, double& average,
    double& uncertainty, double currentvalue,
    ifstream& in, double max, double min,
    int mincount, int maxcount)


    {

    for(n=1; n<=first; n++);
    in >> currentvalue;
    sum += currentvalue;



    if (currentvalue > max){

    max = currentvalue;
    maxcount = n;}


    if (currentvalue < min){

    min = currentvalue;
    mincount = n;}


    average = sum / n;
    uncertainty = (max-min)/n;


    cout << "\n\n" << average << " " << uncertainty;

    system("pause");

    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Odd error from VS 2010 C++

    Quote Originally Posted by antlet88 View Post
    1>------ Build started: Project: Lab 5, Configuration: Debug Win32 ------
    1> data.txt
    1>c:\users\anthony\documents\visual studio 2010\projects\lab 5\lab 5\data.txt(1): error C2059: syntax error : 'constant'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Here is the code. ...
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std; 
    
    void instruction();
    void openfiles (ifstream& in, ofstream& out);
    void process (double& sum, int n, int first, double& average, 
                        double& uncertainty, double currentvalue,
                        ifstream& in, double max, double min, int mincount,
                        int maxcount);
    int main()
    {
    ifstream in;
    ofstream out;
    double sum=0, average, max, min, uncertainty, currentvalue;
    int n=1, first, maxcount, mincount; 
    in >> first;
    in >> currentvalue;
    min = currentvalue;
    max = currentvalue;
    maxcount = n;
    mincount = n;
    
    
    
    instruction ();
    openfiles (in, out);
    process (sum, n, first, average, uncertainty, currentvalue, in, max, min,
                mincount, maxcount);
    
        return 0;
    }
    
    
    
    //Functions---
    
    void instruction()
        
        {
            cout << "***Physics 4AL Data Calculation From Data File.***\n";
                
    
        }
    
    void openfiles (ifstream& in, ofstream& out)
    {
        in.open("data.txt");
    
        if ( in.fail() )
                { cout <<"\nInput file failed to open.\n";
                }
                else {
                    cout << "\nData file opened succesfully.\n";}
        
        out.open("dataout.txt");
    
        if ( out.fail() )
                { cout <<"\nOutput file failed to open.\n";
                }
                else {
                    cout << "\nOutput file opened succesfully.\n\n";}
    
                
    
    
    }
    
    void process (double& sum, int n, int first, double& average, 
                        double& uncertainty, double currentvalue,
                        ifstream& in, double max, double min,
                        int mincount, int maxcount)
    
    
    {
        
        for(n=1; n<=first; n++);
            in >> currentvalue;
            sum += currentvalue;
    
            
    
            if (currentvalue > max){
                
                    max = currentvalue;
                    maxcount = n;}
            
                
            if (currentvalue < min){
                
                    min = currentvalue;
                    mincount = n;}
                
    
            average = sum / n;
            uncertainty = (max-min)/n;
    
    
            cout << "\n\n" << average << " " << uncertainty;
    
            system("pause");
            
    }
    I am curious: do you compile the .txt file?

    Besides, you have to use Code tags to make your code snippets readable. Do you see the difference between your original code snippet and its good formatted variant when using code tags?
    Please, read the Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2013
    Posts
    6

    Re: Odd error from VS 2010 C++

    Sorry about that!!! I'll try it here. Ummm. No I don't think I compile the .txt file. It's just saved in the folder and then called upon in the coding.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std; 
    
    void instruction();
    void openfiles (ifstream& in, ofstream& out);
    void process (double& sum, int n, int first, double& average, 
    double& uncertainty, double currentvalue,
    ifstream& in, double max, double min, int mincount,
    int maxcount);
    int main()
    {
    ifstream in;
    ofstream out;
    double sum=0, average, max, min, uncertainty, currentvalue;
    int n=1, first, maxcount, mincount; 
    in >> first;
    in >> currentvalue;
    min = currentvalue;
    max = currentvalue;
    maxcount = n;
    mincount = n;
    
    
    
    instruction ();
    openfiles (in, out);
    process (sum, n, first, average, uncertainty, currentvalue, in, max, min,
    mincount, maxcount);
    
    return 0;
    }
    
    
    
    //Functions---
    
    void instruction()
    
    {
    cout << "***Physics 4AL Data Calculation From Data File.***\n";
    
    
    }
    
    void openfiles (ifstream& in, ofstream& out)
    {
    in.open("data.txt");
    
    if ( in.fail() )
    { cout <<"\nInput file failed to open.\n";
    }
    else {
    cout << "\nData file opened succesfully.\n";}
    
    out.open("dataout.txt");
    
    if ( out.fail() )
    { cout <<"\nOutput file failed to open.\n";
    }
    else {
    cout << "\nOutput file opened succesfully.\n\n";}
    
    
    
    
    }
    
    void process (double& sum, int n, int first, double& average, 
    double& uncertainty, double currentvalue,
    ifstream& in, double max, double min,
    int mincount, int maxcount)
    
    
    {
    
    for(n=1; n<=first; n++);
    in >> currentvalue;
    sum += currentvalue;
    
    
    
    if (currentvalue > max){
    
    max = currentvalue;
    maxcount = n;}
    
    
    if (currentvalue < min){
    
    min = currentvalue;
    mincount = n;}
    
    
    average = sum / n;
    uncertainty = (max-min)/n;
    
    
    cout << "\n\n" << average << " " << uncertainty;
    
    system("pause");
    
    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Odd error from VS 2010 C++

    Quote Originally Posted by antlet88 View Post
    Sorry about that!!! I'll try it here. Ummm. No I don't think I compile the .txt file. It's just saved in the folder and then called upon in the coding.
    Please, don't post unformatted code! Ever!

    Second, I don't care what you "think" about what type of file you compile. Your snippet from VS compiler output shows file data.txt
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2013
    Posts
    6

    Re: Odd error from VS 2010 C++

    Okay, you're coming off kind of aggressive...Or maybe I'm getting the wrong impression.
    As I said, this is literally my first class in programming. I've never done it before. I just can't figure
    out why I'm getting a syntax error from a .txt file. I am not doing anything to specifically compile
    the .txt document. I just created it and added numbers to ifstream the data into the c++ document.
    I spent about 10 hours today writing this code and really just want some help. Thank you.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Odd error from VS 2010 C++

    Quote Originally Posted by antlet88 View Post
    Okay, you're coming off kind of aggressive...
    Maybe... But it was just because you had ignored what I told you in my first reply...

    Quote Originally Posted by antlet88 View Post
    I just can't figure out why I'm getting a syntax error from a .txt file. I am not doing anything to specifically compile
    the .txt document. I just created it and added numbers to ifstream the data into the c++ document.
    I spent about 10 hours today writing this code and really just want some help. Thank you.

    Well, why do you compile a .txt file, not a .cpp nor a .c one?
    Anyway, have a look at the
    Compiler Error C2059

    BTW, is it the only one error message you got?
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Odd error from VS 2010 C++

    c++ programs normaly have an extension of .cpp. What's the name of the program you are trying to compile? Data.txt seems to be a text file that is used by the program. Somehow VS seems to think it should compile this. It may be that's it's got part of the solution that VS is trying to compile. In VS you should have a Solution Explorer window. In this window you should see a tree of the solution you are trying to produce. Under the branch source files you should see the name of the program you are trying to compile. You should not see data.txt here. If you do, then remove it (but don't delete the file). You should also see branches for header files and resource files etc. gain, data.txt should not appear here. The code you supplied compiles without error so somehow the complier is trying to complile your data.txt file. If you can't see this file name in the soultion explorer, try looking in properties of the project. Somwhere you're told VS to treat data.txt as a source file.

    Hope this helps but trying to work out an issue with the complier is sometimes harder than debugging code!

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Odd error from VS 2010 C++

    That code compiles and links cleanly for me.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Odd error from VS 2010 C++

    When you do get this program to compile, you've got at least a couple of problems.

    Code:
    in >> first;
    in >> currentvalue;
    min = currentvalue;
    max = currentvalue;
    maxcount = n;
    mincount = n;
    instruction ();
    openfiles (in, out);
    You are trying to read from the file before you are opening it.

    Code:
    for(n=1; n<=first; n++);
    in >> currentvalue;
    sum += currentvalue;
    The for loop uses some CPU cycles but doesn't accomplish anything. I think you need to remove the final ; and put the following code in braces as a compond statement.

    Good luck.

  10. #10
    Join Date
    Mar 2013
    Posts
    6

    Re: Odd error from VS 2010 C++

    Thanks everyone who didn't just yell at me for formatting. I'm sure once I become better at programming those things will come a little more natural. I'll try those recommendations tonight. Unfortunately I have several other classes I need to study. I'll try looking in the solution explorer for a file like that, and then make sure I open the files before I use them. Thanks for the tips. Really appreciate it.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Odd error from VS 2010 C++

    Quote Originally Posted by antlet88 View Post
    Thanks everyone who didn't just yell at me for formatting.
    No one was yelling. From the point of view of folks trying to answer it can be a bit frustrating to try to wade through unformatted code and also respond to a post only to have the OP seem to ignore the response. If you want the maximum help from this forum, try to remember to respond to any questions from other posters - even if the question seems to be unrelated. Of course, not every response is going to be on the mark, but as you gain experience, you'll be able to distinguish what's relevant. Actually, as you gain experience, you'll be answering questions, not posting them.

    In terms of your problem, open the solution explorer, right click on the txt file in question, and choose properties. Make sure that the text file is set to be excluded from the build. I'm using VC++ 2012 and I see 3 settings: "Excluded from Build", "Content", "Item Type". In my project, only "Item Type" has a value ('Text'). Earlier versions of VC, might have some settings worded differently.

    Welcome to the site.

  12. #12
    Join Date
    Mar 2013
    Posts
    6

    Re: Odd error from VS 2010 C++

    Hey it worked. I found the setting exclude from project and it builds just fine. I mean the program doesn't actually work but at least it runs. Haha. I think the numbers from the text file aren't being streamed into the program properly because the calculations are all wrong. I fixed the issue with using the text file before I opened it. Oh well. I'll have to hand it in as is. Thanks for all of your help guys. Sorry about the formatting. I'll try and clean it up next time.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Odd error from VS 2010 C++

    If you let us know the format of the text file you are using for input and re-post the program, we may be able to provide further advice for you to get the program working.

  14. #14
    Join Date
    Mar 2013
    Posts
    6

    Re: Odd error from VS 2010 C++

    It's written in this form. Just the value and then hitting enter to the next line.
    Code:
    15
    2.34
    2.43
    2.30
    2.29
    2.41
    2.42
    2.33
    2.31
    2.39
    2.40
    2.32
    2.37
    2.42
    2.38
    2.35

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Odd error from VS 2010 C++

    And what's your program look like now? What's it supposed to output to the text file?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured