Thread: advice for newbies

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    advice for newbies

    Hi y'all.... In a nutshell, I'm in a C++ class and I'm extraordinarily confused about the proper usage and identification of variable declarations, integers, and the proper way to program with mathematic functions (oh, THAT!). For example, working with count, sum, averages and standard deviation as algebraic functions.....

    Because I know better than to ask you to do my homework for me, can anyone recommend some good learning sources for a right-brained person in a left-brain course? Our text is "Programming in C++" by Dale and Weems, 3rd edition. It doesn't impress me with depth of explanation.

    Thanks in advance.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Have you checked out this site's tutorials ?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Excuse me, I don't mean to be a burden or anything but I also have a question. I'm very new to C++ or anything related rather. I just read through the tutorial and used

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
      cin.get();
    
      return 0;
    }
    So this is suppose to open a text dialogue from the command cout? Cin.get() prompts the user to enter a key before closing the program? I'm very confused, if you would have any spare time to point us in the right direction I would be the most of thankful.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    This program when run will open a console. cout directs output to the std output stream which is in this case the monitor. Thus it will cause the message:
    HEY, you, I'm alive! Oh, and Hello World!
    To be displayed.

    The cin.get() is a way to keep the console window open long enough for you to see the result of the program by waiting for the user to press any key.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Okay, so then why when I try opening the .exe file nothing happens? Thank you so much in advance ^_^ going to buy some books on C++ tomorrow

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    If nothing happens, then your compiler isn't functioning properly. Or, you must have messed up coding it. Other than that, I don't know what to tell you.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, you've stumbled into the right room...

    here's some starters:

    a varable declaration looks like this:
    Code:
    type name;
    //for example:
    int myvariable;
    basically, the types you'll most likely be using are char, int, and float. a character is a single character, like 'A','a','B', or 'b'. as you may be able to tell, an 'A' is a different character from an 'a'. an integer is a whole number without a fraction. for example, 1,2,3 are all integers, 1.2 is not. floats are used to hold fractions/decimals, but they can also be used for integers. for example, 1,2.3,4.67 are all valid floating-point numbers.

    first, we'll add. generally, what's on the left of the equal sign (the lvalue) must be the variable the sum will be saved to. this variable can also be a part of the addition. here are some examples:
    Code:
    int a,b,c,d,e;
    a=b+c;  //add b and c and save it to a
    a=a+d; //add a and d and save it to a
    a+=e;   //add a and e and save it to a
    a++;     //add one to a and save it to a
    ++a;     //add one to a and save it to a
    now, to count something, you will most likely use an integer and start by setting it to zero, then adding 1 to it every time something happens. a variable used like this is called an accumulator. for example:
    Code:
    int counter=0;  //create an int called counter and set it to 0
    
    for(int i=0;i<10;i++)  //a loop structure - will go around 10 times
    {
        ++counter;  //increase counter by 1.  it's the same as counter=counter+1;
    }
    with averages, you want to add all the values and divide by the amount there is. one simple way to do this is like this:
    Code:
    #include<iostream>
    
    int main()
    {
        int score=0;    //this will be the accumulator - start at zero
        int temp;       //this is to hold each individual score
        int counter=0;  //this is to count the nubmer of scores
        
        for(int i=0;i<5;i++)  //a loop structure - will loop 5 times
        {
            std::cout<<"> ";        //output a prompt
            std::cin>>temp;         //take in an integer
            std::cin.ignore(1);
            
            score+=temp;    //add the value taken in to the rest of them
            counter++;      //increase the number of scores taken in
        }
        
        score/=counter; //divide the total scores by the amount taken in
        std::cout<<"Average: "<<score<<std::endl;   //output the average
        std::cin.get(); //wait for the user to press enter
        return 0;       //exit with a success value
    }
    Last edited by major_small; 02-14-2005 at 10:33 PM. Reason: [i][/i] and [/COLOR]
    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

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Thank you for that guide major_small, as for myself I figured out what my problem was, i'm such a dunce.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Our text is "Programming in C++" by Dale and Weems, 3rd edition. It doesn't impress me with depth of explanation.
    I think when you are learning any language, you need two books: a main book that you study exclusively, and another one to look up confusing stuff. I can recommend "Ivor Horton's Beginning C++". You can get it used in good condition for only $16 here(which is a great deal for a 900 page book):

    http://dogbert.abebooks.com/servlet/...imagefield.y=0

    Check the shipping charges--all the sellers charge different shipping, so the cheapest price may not be the cheapest book.

    It also sounds like you have two issues: the math and the language. You have to have the math clear in your head before you start trying to write a program.
    Last edited by 7stud; 02-14-2005 at 11:32 PM.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Declaring variables and simple arithmetic:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        
        int num1;  //declare integer variable num1
        num1 = 10;  //assign a value to the variable num1
        cout<<num1<<endl;
    
        int num2 = 20;  //declare the variable and assign it a value
        cout<<num2<<endl;
    
        num1 = 100;
        cout<<num1<<endl;
    
        int sum = num1 + num2;  //sum equals 120
        cout<<sum<<endl;
    
    
        char ch = 'a'; //single quotes are a must
        cout<<ch<<endl;
    
    
        double d = 10.4;
        cout<<d<<endl;
    
    
        //Integer arithmetic chops off the decimal part
        int a = 5, b = 2;
        int sum2 = a/b;  //sum2 equals 2
        cout<<sum2<<endl;
    
        //decimal arithmetic
        double x = 5.0, y = 2.0;
        double sum3 = x/y; //sum3 equals 2.5
        cout<<sum3<<endl;
    
        return 0;
    
    }
    Simple function:
    Code:
    #include<iostream>
    using namespace std;
    
    void func1()
    {
        cout<<"hello"<<endl;
    }
    
    int main()
    {
        func1();
        
        return 0;
    
    }
    Functions must have a return type listed before the function name. In this case, the function doesn't return anything, so it's return type is 'void'--meaning 'nothing'. The function is executed in main with the statement: func1();

    Simple function, 1 'parameter'
    Code:
    #include<iostream>
    using namespace std;
    
    void func1(int a)
    {
        cout<<a<<endl;
    }
    
    int main()
    {
        func1(3);
        
        return 0;
    }
    This time the function has a 'parameter'. That means the function expects a number will be sent to it. The 'parameter' is a variable that stores the number sent to the function. Then, inside the function the number will be used in some way. In main, the function is called with this statement:

    func1(3);

    and the number that the function is expecting is put between the parenthesis--you can think of it as emailing the number to the function. The number you put between the parentheses must match the type of the parameter listed for the function--in this case the parameter is int a. The integer number 3 is sent to the function, and it is stored in the variable a. When the function receives the value, it displays 'a' using cout.

    Simple function, 1 parameter, returns a value
    Code:
    #include<iostream>
    using namespace std;
    
    int func1(int a)
    {
        int sum = 2 * a;
        return sum;
    }
    
    int main()
    {
        int result = func1(3);
        cout<<result<<endl;
        
        return 0;
    }
    This time the function returns a value. It is not returning 'void' anymore, so in front of the function name, you have to indicate the return type. Inside the function, the return statement returns sum, which is of type int, so the return type of the function must be int. Since the function is returning a value, you want to capture that value when you call the function in main(). I declared the variable 'result' to capture and store the return value of func1(). 'result' must have the same type as the function's return type.
    Last edited by 7stud; 02-14-2005 at 11:56 PM.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Wow we are sure getting a lot of members?!
    My computer is awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  3. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 01:06 PM
  4. need hardware advice....
    By forgottenPWord in forum Tech Board
    Replies: 5
    Last Post: 03-23-2003, 09:12 AM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM