works on windows not linux maybe std?

This is a discussion on works on windows not linux maybe std? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> int main() { int total, gradeCounter, grade; float average; total = 0; gradeCounter = 0; ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    28

    works on windows not linux maybe std?

    Code:
    #include <iostream>
    #include <iomanip>
    
    
    int main()
    {
        int total,
            gradeCounter,
            grade;
        float average;
    
        total = 0;
        gradeCounter = 0;
        
        cout << "Enter grade, -1 to end:  ";
        cin >> grade;
    
    
        while (grade != -1 ) {
            total = total + grade;
    	gradeCounter = gradeCounter + 1;
    	cout << "Enter grade, -1 to end:  ";
    	cin << grade;
    
        }
    
        if ( gradeCounter != 0 ) {
        average = static_cast< float >( total )
        cout << "Class average is " << setprecision( 2 )
             << setiosflags ( ios::fixed | ios::showpoint )
    	 << average << endl;
        }
        else
          cout <<"No grades were entered" <<endl;
    
        return 0;
    }
    Hi i made this program and i am only doing c++ a tiny while.

    I am compiling on gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

    Thanks alot guys/girls
    I hope to be a regular.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >#include <iostream>
    >#include <iomanip>

    You need to add this here:
    using namespace std;

    >cin << grade;
    This should be:
    cin >> grade;

    >average = static_cast< float >( total )
    To calculate the average:
    average = static_cast< float >( total ) / gradeCounter;

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Umm... I can't really tell what your question is.

    From the title, and a look at your code, I'd guess that its not working, and that you need this line right after your includes:
    Code:
    using namespace std;
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Hey thanks so much guys.

    All i want to do when learning is try have it os independant so it works now on win32 and on linux.

    Thanks alot works grat.

    So in future i will put in using namespace std;

    I never learned this yet so thanks.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    after you get that down, try learning some of the parts of the namespace... eventually you'll want to only declare what you need, like this:
    Code:
    using std::cin;
    using std::cout;
    using std::endl;
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Or even better, omit the using entirely and just use std::cout, std::map, std::endl, etc.


    Once you get beyond trivial programs, there really aren't that many of them, and when you're working with multiple libraries (I am up to using 12 libraries on one project) it's great to see at a glance which library you're using.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    135

    Re: works on windows not linux maybe std?

    Some other points to consider:
    Code:
        int total,
            gradeCounter,
            grade;
        float average;
    
        total = 0;
        gradeCounter = 0;
    You declare all of your local variables at the start of the function, C-style, which is unnecessary in C++. You can declare them when you need them, which is more in keeping with C++ style. Also, instead of creating them and then initialising them on the next line with an assignment, just initialise them when you declare them. Generally people use double for floating point values unless they have a specific reason to opt for float.

    Both of these lines do what you want, but are not typical C++ style.
    Code:
    total = total + grade;
    gradeCounter = gradeCounter + 1;
    Consider the following:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        cout << "Enter grade, -1 to end:  ";
        int grade;
        cin >> grade;
    
        int total = 0;
        int gradeCounter = 0;
        while (grade != -1 ) {
            total += grade;
            gradeCounter++;
            cout << "Enter grade, -1 to end:  ";
            cin >> grade;
        }
    
        if ( gradeCounter != 0 ) {
            double average = static_cast<double>( total );
            cout << "Class average is " << setprecision( 2 )
                 << setiosflags ( ios::fixed | ios::showpoint )
    	         << average << endl;
        }
        else
            cout << "No grades were entered" << endl;
        return 0;
    }
    Note particularly how average is only declared within the block in which it is used, in the smallest scope required. In fact, unlike the original code, if gradeCounter equals 0 the program doesn't need to create average at all. No big deal for a single instance of a double, but more of a consideration if it's a large object with a busy constructor, perhaps inside a function that is called many times - can become an expensive operation for something that isn't used.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Cat..

    and when you're working with multiple libraries (I am up to using 12 libraries on one project) it's great to see at a glance which library you're using.
    How do you mean to see at a glance?





    Code:
    int total,
            gradeCounter,
            grade;
        float average;
    
        total = 0;
        gradeCounter = 0;
    I did it like this as i am teaching myself from a book.
    And i find intiliasing the variables is easier for me to not get mixed up.
    Also double for floating point values i haven't learned this yet but i think their is not point in using double if it takes up more memory?
    As the higest number can only be 100 i think .


    Code:
    total += grade;
    i am jsut learning these parts now.

    Good tip.

    Code:
    gradeCounter++;
    this part also i'm just begining.

    I thank you all for your help guys/girls.
    Much appreciated.
    Cya.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    I appreciate you're just learning, and you're doing fine. You don't have to adopt the things I said at this time, mostly it's just a question of style, but you may wish to come back to those points later.

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by blackgold>>
    Cat..
    How do you mean to see at a glance?
    By not making use of "using", when you declare variables, you can see exactly which library you're using.

    E.g.:

    boost::shared_ptr<std::vector<int> > pVector;

    This would tell me at a glance I was using Boost for a smart pointer, and a vector from the standard library.

    I rarely use using, except for a few cases. One is where I want to expose a base class member that would otherwise be hidden. Others are where it saves a substantial amount of typing and greatly improves clarity. Even in this case, I limit the scope of using as much as I can.

    Here is an example:

    Code:
    void TensorMath(){
      blitz::Array<double,2> x;
      blitz::Array<double,2> y;
      blitz::Array<double,2> z;
    
      x = 1, 0, 2, 2,
            2, 1, 0, 1,
            1, 3, 4, 1;
    
      y = 2, 3, 6,
            8, 3, 1,
            0, 1, 0,
            5, 5, 3;
    
      {
        using namespace blitz::tensor;
        z = sum(x(i,k) * y(k,j),k);
      }
      // The above lines are equivalent to the tensor equation
      //  ij    ik  kj
      // z   = x   y
    }
    In the above, the using statement (which ONLY affects the statement just blow it because of scope issues) means I can use i,j,k,l, ... in the expressions and it understands what I mean. It greatly simplifies the appearance.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread and socket porting from Linux to Windows
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 07-18-2008, 06:57 AM
  2. Linux vs Windows
    By manav in forum A Brief History of Cprogramming.com
    Replies: 114
    Last Post: 04-08-2008, 08:32 AM
  3. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 02:36 PM
  4. Linux and Windows Duel Boot
    By The15th in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-26-2002, 04:59 AM
  5. linux vs. windows
    By muttski in forum Linux Programming
    Replies: 18
    Last Post: 04-07-2002, 09:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21