Thread: newb question~

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    newb question~

    I'm using Dev-C++ to practice code from the first tutorial on this site, I can get the first one to work the one that is ran in Console Application. I cannot get the other ones to work although they seem to be the same and im pretty sure im not leaving things out because i copy and paste. I was wondering if theyre supposed to be in something else than console application? If anyone knows or wants to help me my aim is Anima46i2 any help is greatly appreciated thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I cannot get the other ones to work
    Can you be more descriptive? What doesn't work exactly? Care to paste some code too (in code tags).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    yes, this code for instance i got to work but it doesnt seem to me to work properly.

    Code:
    #include <iostream.h>
          int main() { int thisisanumber; cout<<"Please enter a number:";
    cin>>thisisanumber; cout<<"You entered: "<<thisisanumber;
    return 0; }
    it brings up a window that says please enter a number. when you enter a number i guess the logical thing to do is to press enter, but this closes the window.

    When talking about variables the tutorial says

    int - a,b,c,d . I thought int stood for numbers? i dont understand that.

    Char - letter. I thought char was supposed to be a single character like "a" or "b"

    Float - The_float . I thought float was supposed to be a number with a decimal point

    this is all very confusing 8( i dont understand alot of other things but rereading the tutorial hopefully will help me with that and i dont want to bore you .

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First up, read this:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    >>int - a,b,c,d . I thought int stood for numbers? i dont understand that.<<
    Well, yes int is for numbers in essence, but you can name your variables anything you like (within the rules). Also characters are actually numbers in disguise anyway, checkout the ASCII chart to see what I mean.

    >>Char - letter. I thought char was supposed to be a single character like "a" or "b"<<
    Yes, a char holds a single byte, or one character.

    >>Float - The_float . I thought float was supposed to be a number with a decimal point<<
    Yes, that's basically correct.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    Sir, how is the_float a number with a decimal? K ill read the faq, heh i know im supposed to read before posting but thats along faq and my eyes hurt from reading so much 8(

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    double post 8(

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    to answer your question about floats (and other variables at the same time too i guess), you can name a float type variable anything you want (almost) but it's value has to be a number with a decimal place, for example, you could have this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       float the_float = 13.4;
       cout<<the_float;
       int a_number = 4;      //etc. same with other variables
       cout<<a_number;    
    
    return 0;
    }
    But you couldn't have this:
    Code:
    int main()
    {
      float a_float = "this is a float";           //Because a_float has to
      int a_number = "this is a number";    //equal a number with a      
     return 0;                                             //decimal point.
    }                
    /*It's the same with any other variable, it can be called almost anything, but it has to equal it's type, ie. an int has to equal a number, a char has to equal a character (unless it's a string, but don't worry about that yet)  */
    Edit: Woah sorry my post stretched out so far to the right, but I don't know how to fix it.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    i meant that my program closes whenever it asks for input like in the "please enter a number one" when you enter a number you cant enter it because it will close . I know how to keep the window open until a key is pressed...its just that i dont want it to close after the key is pressed .

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    The faq has a topic on how to keep the window open. Essentially, your program finishes so the window closes. The output was displayed, but it all happened so fast, it looks like it didn't work. Put these lines as the last lines of your program:

    cin.ignore();
    cin.get();

    and the window will stay open so you can see the output.

    int - a,b,c,d . I thought int stood for numbers? i dont understand that.

    A variable name can be anything:

    a
    aaa
    my_int
    Aardvaark
    MyReallyNeatVar
    dog
    number3

    and the variable name does not indicate the type of the variable in anyway. So this statment:

    int a, b, c, d

    declares four int variables with the names:

    a
    b
    c
    d

    Just because the variable names above are single letters does not mean they contain char's. Remember a variable name can be anything and does not indicate what type it is. Similarly a char variable can be named anything too:

    ch687
    ACharVar12
    dog76
    letter54
    MyTestScore
    a1000
    z

    and if all of those variables are declared as type char, they will only be able to store one char. Finally, for floats the same rules apply. The variable name can be anything:

    MyFloat
    a
    x
    y
    anInterstingVar
    MyTestScore

    but if those variables are declared floats, then they can contain decimal numbers. Generally, you will name your variables with descriptive names. If you need to declare a variable in order to store a char representing a test score: 'A', 'B', or 'C' you might declare your variable like this:

    char EnglishGrade;

    If you need an int variable to store the number of students in a class, you might declare your variable like this:

    int NumberOfStudents;

    If someone said, "I have 4 variables in my program and here are their names:

    x
    y
    ch
    AVariable"

    Then the person asked you: "What types are those variables?" Your answer would be: "It'is impossible to tell, mister." If he showed you variable names like this:

    ThisIsAFloat
    ThisIsAChar
    ThisIsAnInt

    You still could not tell him what the variable types were because he could have declared them like this:

    char ThisIsAFloat;

    float ThisIsAnInt;

    int ThisIsAChar;

    That would be a stupid way of naming his variables, but it would be entirely legal.
    (Note: technically a variable name can't be ANY sequence of characters--C++ has certain rules about variable names, but they are pretty liberal. As long as you begin the name with a letter, and then use letters, digits or an underscore(e.g.: MY_dog7) after that, you will be fine)
    Last edited by 7stud; 08-23-2003 at 10:25 PM.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    edit: ok that was a stupid question but here's another ~ maybe even stupider .

    can anyone point out to me why this code doesnt work?

    Code:
              #include <iostream.h>
    int main()
    {
     char y/n;
     cout<<"Do you love branden hunter?:"
     cin>>y/n;
     if(y/n==y)
     {
      cout<<"You're awesome !"
      }
      else if(y/n==n)
      {
      cout<<"I'd approve if you died" ;
      }
      else
      {
      cout<<"you cant follow simple directions and id approve if you died";
      }
      return 0;
    }

    i did it like the tutorial but with char instead of int and with the code the guy gave me to keep the window open ~

    edit: i forgot the code tags
    Last edited by Anima; 08-24-2003 at 03:04 PM.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's your code with the syntax errors removed. Note that a variable name cannot contain a /.

    Code:
    #include <iostream.h>
    int main(void)
    {
      char yn;
      cout << "Do you love branden hunter?:";
      cin >> yn;
      if (yn == 'y')
      {
        cout << "You're awesome !";
      }
      else if (yn == 'n')
      {
        cout << "I'd approve if you died";
      }
      else
      {
        cout << "you cant follow simple directions and id approve if you died";
      }
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. Dogpile the newb!
    By lindy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-23-2008, 08:17 AM
  3. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 01:45 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. Newb With Small Pointer Issue
    By G-Prime in forum C Programming
    Replies: 7
    Last Post: 09-06-2004, 04:09 PM