Nothing but newbie.

This is a discussion on Nothing but newbie. within the C++ Programming forums, part of the General Programming Boards category; Hey guys.. I am, as some may say, a C++ "newbie", in that I can only do some very simple ...

  1. #1
    Jimmey
    Join Date
    Jul 2005
    Location
    Manchester, England
    Posts
    6

    Nothing but newbie.

    Hey guys..

    I am, as some may say, a C++ "newbie", in that I can only do some very simple things with my very basic knowledge of this programming language.

    I have, slowly but surely, gotten progressively more functional as a C++ programmer, but, the slowly part of this sentance, I am afraid, should take dominance.

    So, my ability to make programs is seriously laughable, and there are limitations to the amount I can learn just by reading internet tutorials.

    What I am looking for is not someone that will teach me how to become a C++ programmer - just someone that is willing to help me, answer some of my questions, and mostly, tell me what I am doing wrong.

    If you think that you could help, please feel free to get in touch with me. If my email adress isn't displayed anywhere else on this page, then it is:

    jimmey1000@hotmail.com

    If you think that it was a waste of time reading this post, then I'm sorry, but I am still appreciative that you did.

    Thanks

    Jimmey

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well you can post all your questions here, whether they are stupid or not, everyone has been a newbie before.

    And you will get more than just "someone", you'll get the help from tons of programmers.

  3. #3
    Jimmey
    Join Date
    Jul 2005
    Location
    Manchester, England
    Posts
    6
    Thanks =-)

    Nice to know that people are willing to help.

    I think that you may be seeing many questions from me in the near future....

    And, I am looking forward to learning!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am, as some may say, a C++ "newbie"
    Though n00b is more common.

    >my ability to make programs is seriously laughable
    Dedication is everything. If you stick to it, you can make up for any lack of talent with persistance. I'm a good example of that. Despite what some people will say, I have no talent for programming. I don't think like a programmer, and I have a very hard time learning new concepts. But, I still manage to get by through hard work.

    >just someone that is willing to help me, answer some of my
    >questions, and mostly, tell me what I am doing wrong.
    You're in the right place. There are dozens of highly qualified and active programmers here who will be happy to help you.

    >If you think that you could help, please feel free to get in touch with me.
    Unfortunately, we don't do that here. Any Q&A is strictly public by posting to threads such as this one. That's the best way because even if someone accidentally gives you bad advice, they can be corrected. That kind of check and balance system isn't available if you talk to just one person through email.
    My best code is written with the delete key.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah, what JoshR said.

    Getting help from the community gets you explanations from different views, and mostly always a reply no matter how odd the question. Plus sometimes individual programms forget things or give incorrect information, and theres a community to spot it incase its incorrect. Barely any programmers can get all their information correct and explain it, or would want to do such a thing. So unless you get one of those guru's with 3000+ posts at your call, its just as good to post everything here.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Jimmey
    Join Date
    Jul 2005
    Location
    Manchester, England
    Posts
    6

    Thanks.

    Okay - Thanks, again.

    I wanted to refrain from the term "n00b", because I have noticed, and experienced, how when people do replace letters with numbers and symbols, it tends to make them look a little bit lame. I guess that that only applies to when someone does that in excess.

    I thought that, now you guys have introduced me to the "C board", I would put my first questions to you.

    My first question is -
    Can you use an "If" statement within another "If" statement? Or should I leave all of them sperate?

    Second -
    I have been making many small projects, but the focus of my learning has fallen upon trying to make a username and password recognition program. So far, I have been able to make a program that recognises integer usernames and passwords.
    It took a while even for me to make this work, so I was quite pleased when I did. But - I found a bug.

    Heres some code from the program -

    Code:
    int userName;
    int userNameActual = 4158;
    int passWord;
    int passWordActual = 5000;
    cout << "Please enter your username: ";
    cin >> userName;
    bool validUser = ( userName == userNameActual );
    Now, all this stands up fairly well in all my "If" statements, but, reacts weirdly if, instead of inputting an integer, I input a string of characters. Is there any reason for this?

    Third and final -

    As I have mentioned, I am trying to make a password and username recognition program.
    So far, I have used integers as usernames and passwords.
    But, I was wondering how I would use strings, replacing the integers. Whenever I have tried, the I have never been able to predefine a sting of characters in the program that matches the user input.

    Here's some code, to show you what I mean.

    Code:
    char userName[26];
    char userNameActual[26] = "James";
    bool validUser = ( userName == userNameActual );
    When I display "validUser", weather I have entered the username correctly or not, it always returns the value 0.
    What am I doing wrong?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it tends to make them look a little bit lame
    "leet speak" is entertaining when used sparingly and in a tasteful manner. Unfortunately, most people don't know where the line is and cross it on a regular basis.

    >Can you use an "If" statement within another "If" statement?
    Yes, you can nest any of the control structures as much as you want. Technically there are limits, but the standard specifies limits that you would be insane to approach (something like 127 levels) and implementations usually allow for much more.

    >Is there any reason for this?
    Yes, actually. Your call to cin's >> operator expects a valid integer. If you type characters that fail to meet that expectation, cin enters a failure state and the contents of the variable remain unchanged. In this case, the value is unpredictable because you don't initialize it to anything. So the program reacts strangely because you're working with unpredictable values.

    The nice thing about the >> operator is it knows what type of variable you're giving it, and it expects input that can be represented by that type. That's also a problem if, as you've experienced, the user of the program gives it the wrong input. If you want to accept any kind of input (because the stream works with characters and any type conversions are made by >>), you would be better off using a string variable and getline:
    Code:
    #include <iostream>
    #include <string>
    
    std::string name;
    std::string pass;
    
    std::cout<<"User ID: ";
    std::getline ( std::cin, name );
    std::cout<<"Password: ";
    std::getline ( std::cin, pass );
    >it always returns the value 0.
    This is a sticky area, which is why I suggested you use string objects instead of arrays. The problem is that (and if you don't understand this, just keep it on the backburner for later) most of the time, when an array name is used in an expression, it's converted to a pointer to the first element. So your comparison is actually comparing the value of two pointers, which is comparing two addresses. Since the pointers don't point to the same location in memory, the test fails.

    The string class overloads the == operator, so it does what you would expect:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string name;
      std::string pass;
      const std::string valid_name = "jwalker";
      const std::string valid_pass = "meep";
    
      std::cout<<"User ID: ";
      std::getline ( std::cin, name );
      std::cout<<"Password: ";
      std::getline ( std::cin, pass );
    
      if ( name == valid_name && pass == valid_pass )
        std::cout<<"Hello, "<< name <<"!\n";
      else
        std::cout<<"Invalid login\n";
    }
    My best code is written with the delete key.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yes, its legal to use if statements within if statements and necessary in many cases.

    You're using c-style strings when you make a char array, and those require you #include <cstdio> (in C its stdio.h) in order to use functions which are required if you want to do anything with the strings. Theres a tutorial on the main site that will answer your question on c-style string: http://www.cprogramming.com/tutorial/lesson9.html

    Code:
    int comparison;
    char string1[] = "alpha";
    char string2[] = "beta";
    
    comparison = strcmp (string1, string2);
    That would be how to compare c-style strings (according to google.com). But this is C++, so you can use 'string' instead, which is a much more simple looking syntax. Theres a tutorial on them on the main site: http://www.cprogramming.com/tutorial/string.html
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Jimmey
    Join Date
    Jul 2005
    Location
    Manchester, England
    Posts
    6
    Okay, thanks guys.
    I think that I will look a little more into strings - I think that I should have done in the first place.

    In regards to that "If" statement question -
    Whenever I do try to use an "If" statement within another, it refuses to compile.

    Can you give me an example of how I would do this?

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    if (cinUsername == realUsername) {
    
      cout << "Access Approved.";
      if (somethingElse == 1) {
        //do something
      } else { 
    
      }
    
    } else if(cinUsername == 0) {
      cout << "You must enter a username.";
    } else {
      cout << "Access Denied";
    }
    Excuse my unimaginative situation where you would want an if statement within an if statement. BTW might want to post your code (since an if statement isnt that long) so we can just see whats wrong with it.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    just make sure it has correct syntax:
    Code:
    if (something) 
    {
       if (something else)
       {
           if (something else)
          {
          
          }
       }
    }
    Sometimes its easier to line up the brackets instead of K&R type brackets (which I do sometimes), but before you get the hang of it, try lining them up to make sure.
    Last edited by JoshR; 07-04-2005 at 12:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 07:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 08:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

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