Question about variables

This is a discussion on Question about variables within the C++ Programming forums, part of the General Programming Boards category; I was just thinking! Well lets say I want to make a simple program that if you enter the password ...

  1. #1
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321

    Lightbulb Question about variables

    I was just thinking! Well lets say I want to make a simple program that if you enter the password you get an answear you entered right password.

    So this is what i come up with:

    Code:
    #include <iostream.h>
    
    int main()
    
    {
    
    int password == tiger;
    	cout<<"Enter password";
    
    	cin>>password
    
    }
    
    if (password == tiger)
    {
    	cout<<"Password is right";
    }
    
    else
    {
    	return 0;
    }
    So can I use variable as letters? or they only must be numbers?
    From Ukraine with love!

    Internationally known widely respected

    - Digitally yourz -

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You seem very mixed up. This piece of code is awful, you have made more mistakes in 8 lines than microsoft made in windows 3.1 lol. Maybe you should consider some web tuts or a good book.
    an int is a variable type to represent integers that is whole numbers.
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        char password[]={"tiger"};
        cout<<"Input password :-";
        char buffer[256]={0};
        cin.getline(buffer,256);
        if (strcmp(buffer,password)==0)
            cout<<endl<<"Thats the correct password!"<<endl;
        else 
            cout<<endl<<"Thats incorrect!"<<endl;
        return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Unhappy

    Your code is indeed a disaster, but if you're new at this i think that stoned_coder's code is a little to advanced try this instead:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string password="tiger";
    
       cout << "Enter Password:"
       cin >> password;
    
       if(password=="tiger")
       {
          cout << "Password is correct";
       }
       else
       {
          cout << "Password is incorrect";
       }
    
    
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    variables can be anything you want, as long as their type is declared appropriately. Some variable types are predefined, like int or double or char. Others you make yourself using typedef or class or struct declarations. a variable of type int can only hold an int and not a variable of type char. You can sometimes cast (temporarily use a value of one type as a different type) from one type to another.


    int num;
    //assign a literal integer to num
    num = 4;

    int num2;
    //assign another variable to num2
    num2 = num;


    double num3;
    //cast an int to a double and assign it to num3
    num3 = (double)num2;//I haven't committed new cast syntax to memory yet.

    The picture gets a little cloudier when you learn that char types are actually integers to the compiler (converted from one to the other by a character set) and can sometimes be used as type int(when you know what you are doing), and by things like unions--which are variables of one type under one condition and might be other types under other conditions, etc.; but those discussions are for later in your journey.

  5. #5
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321
    Originally posted by Travis Dane
    stoned_coder's code is a little to advanced
    I just want to correct you! its supposed to be too not to!

    But anyway thanks for the code! It helps! But I got another question about stoned_coder's code. Travis, you are right about me being a newbie.

    So I went through the stoned_coder's code and found that he brought up another preprocessor #include<cstring>.

    And what are those "buffer,256" In lines 8 and 9?
    From Ukraine with love!

    Internationally known widely respected

    - Digitally yourz -

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You won't really understand this code until you get
    to pointers and arrays. Best to just read
    http://www.cprogramming.com/tutorial.html or a book
    on the subject.

  7. #7
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    *tear*

    The cprogramming.com tutorials bring back so many memories. That's where I learned. I started C++ because I downloaded Wolfenstein 3D's engine b4 I even knew what C was and, as a lot of you know, Wolfy was made in C and Asm. Well I started making little changes like changing text around and junk, and I started looking through the code and used context clues to figure out what diff things did and I decided one day "Maybe I can try to make something small". Of course I couldn't because wolfy was too advanced for a novice, and uses NO standard text stuff like printf() (well it uses it, but not like a normal dos proggie) so I basically was stuck there trying to figure out what to do. So I did a search and found this site, and saw C++, and I was like, Cool! A newer version! So I downloaded Bloodshed's Dev-C++ 4.05 (it was the newest version at that time) and couldn't get it to use clrscr() so I switched to DJGPP. Hey, I was a n00b, I didn't know you could just do a system call to cls. I used that for a while until I got Allegro. I used allegro with DJGPP for like a week until I found out it works the same in windows, so I switched back to Dev-C++4.05

  8. #8
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    Some tuts like: Talk to like to a three old:

    http://www.gametutorials.com/

    Some other tutorials:

    http://www.tutorials.com/

    Have fun learning C++

    But it's more fun after having enough knowledge and progging good stuff *g

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I worse though. I ended up trying to edit wolf3d.exe with ms edit. This was way before I knew any programming
    languages.

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Another good resource for you is at cplusplus.com they have an excellent beginner's guide and reference on C++.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    here ill break it down for you.

    #include<iostream>
    #include<cstring>
    using namespace std;

    is almost exactly the same as..
    #include<iostream.h>
    #include<string.h>
    Code:
    int main()
    {
        char password[]={"tiger"};
        cout<<"Input password :-";
        char buffer[256]={0};
        cin.getline(buffer,256);
        if (strcmp(buffer,password)==0)
            cout<<endl<<"Thats the correct password!"<<endl;
        else 
            cout<<endl<<"Thats incorrect!"<<endl;
        return 0;
    }
    First line declares an array of chars calls that array password and stores the string "tiger" in the array.The array will be six chars in length. 5 for the password and 1 for the '\0' that marks the end of the string.
    Then a simple cout.
    Next i have declared a char array of 256 chars called buffer and initialised it all to 0.This is where we will store the input.
    cin.getline() gets a string including whitespace up to a delim char which if not specified is taken to be '\n'. So this will read from standard input(keyboard) up to a newline or 255(saves 1 for null byte) chars and store it all in buffer.
    the strcmp() function call compares 2 strings and if they are lexicographically identical then it returns 0. So if strcmp returns 0 we print that strings match else we print that they dont match. End of small prog. Time to return 0 to the operating system to signal no errors.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  4. question about declaring global variables
    By Dag_ in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 06:03 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM

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