My first C++ program!

ray_MAN

Tank
Joined
Aug 14, 2003
Messages
4,655
Reaction score
2
This is my first C++ program I have ever made without a tutorial!
Tell me what you think: :D
PHP:
#include <iostream.h>

int main()
{
    int a,b;
    cout <<"Username: ";
    cin >> a;
    if(a == 748230)
    {
       cout<<"Password: ";
    }
    else
    {
       cout<<"Password: ";
    }
    cin >> b;
    if(b == 9999)
    {
        cout<<"Welcome 748230!";
    }
    else
    {
        cout<<"-=ERROR=-Incorrect username or password! Please hit F9 to try again.";
    }
    return 0;
}

Also can someone tell me how to make the password and username a word? I have tried many things. :D :D
 
Hey congradulations.

I'm a begginer when it comes to programming myself, but why do you have an "if" statement where both outcomes display "Password:"?
 
Because if you don't type in '748230' then it still goes to the password request. If I didn't have that, and I typed in 648230 I would never get the password request. I waited to have the error message come up after the whole kitten capudole.
Hope that clears it up! :cheers:
 
ya but no matter what the user enters will result in a request for the password popping up won't it?
 
Yes, it is simple.

PHP:
#include <string>
#include <iostream>
using namespace std;

int main()
{
     string name = "", password = "";
    cout<<"Please enter user name and password:  ";
    cin>>name>>password;
	if(name == "john" && password == "123")
		cout<<"thank you for logging in.  You have security access L3.\n\n";
	else
		cout<<"failed to log in.  aborting.\n";
		
	return 0;
}
 
Exactly what I wanted. If you have VC++ can you compile that in there? I used C++BuilderX. :D
 
Nevermind, someone gave you a better example

Good luck on learning C++, ray. I've been doing it for about a week myself
 
It would be a good idea to use cin.getline as well, as it is safer.

Here's an example, I'v used an array of chars for a string. All the program does is find whether the user input a float or integer.

PHP:
#include <iostream>

//This program tests strings

using namespace std;

short cnvStr(char str1[100], int& a, float& b);

int main()
{
    char buffer[100];
    int outputA;
    float outputB;
    int IFE; //option: 1 for int, 2 for float, 3 for error
    char quit[10];
    
    do 
    {
        cout << "This program will convert strings into numbers\n";
        cout << "Enter a value: ";
        cin.getline (buffer, 100);
        IFE = cnvStr(buffer, outputA, outputB);
        switch (IFE)
        {
                case 1:
                      cout << "\nYour input has been coverted to a Int\n";
                      cout << "Input = " << outputA;
                      break;
                case 2:
                      cout << "\nYour input has been converted to a float\n";
                      cout << "Input = " << outputB;
                      break;
                case 3:
                      cout << "\nYour input has an error in it\n";
                      break;
                default:
                      cout << "\nThere has been an internal error\n";
         }   
         cout << "\nDo you want to quit? ";
         cin.getline (quit, 10);
         cout << "\n";
     } while (quit[0] != 'y' && 'Y');
      
     cout << "Program Termination";
     return 0;
}

short cnvStr(char str1[100], int& a, float& b)
{ 
    short I;
    bool floatNum = false, EOS = false;
    int IFE;
    
    for (I = 0; I <= 100; I++)
    {
        cout << "\nI == " << I;
        switch (str1[I])
        {
                case '\0':
                {
                    EOS = true;
                    break;
                }
                case '.':
                {
                    if (floatNum)
                    {
                        cout << "\nSecond decimal point found";
                        IFE = 3;
                        return IFE;
                    }
                    else
                    {
                        cout << "\nDecimal point found";
                        floatNum = true;
                    }
                    break;
                }
                case '0': cout << "\nfound 0"; break;
                case '1': cout << "\nfound 1"; break;
                case '2': cout << "\nfound 2"; break;
                case '3': cout << "\nfound 3"; break;
                case '4': cout << "\nfound 4"; break;
                case '5': cout << "\nfound 5"; break;
                case '6': cout << "\nfound 6"; break;
                case '7': cout << "\nfound 7"; break;
                case '8': cout << "\nfound 8"; break;
                case '9': cout << "\nfound 9"; break;
                default: 
                {
                    IFE = 3;
                    return IFE;
                }
        }
        if (EOS)
        {
            cout << "\nEND OF STRING\n";
            break;
        }
    }
    
    if (floatNum)              // ((floatNum) && (IFE != 3))
    {
        b = atof(str1);
        IFE = 2;
        return IFE;
    }
    if (!floatNum)            //((!floatNum) && (IFE != 3))
    {
        a = atoi(str1);
        IFE = 1;
        return IFE;
    }
    return IFE;
}
 
btw going back to your first example, The user doesnt have to enter the right Username, any username and the right password will cause that to work. Dunno if u intended that, just thought I'd point it out, what u actually wanted was

Code:
#include <iostream.h> 

int main() 
{ 
    int a,b; 
    cout <<"Username: "; 
    cin >> a; 

       cout<<"Password: "; 
    cin >> b; 

    if(b == 9999 && a == 748230) 
    { 
        cout<<"Welcome 748230!"; 
    } 
    else 
    { 
        cout<<"-=ERROR=-Incorrect username or password! Please hit F9 to try again."; 
    } 

    return 0; 
}

Look thru your first code and see how the logic is wrong (think what happens if u enter the wrong u/n :) )
 
I think he is just learning, so the code he has written is fine. Don't forget the title of the thread:

"My FIRST c++ program..."
 
EvNTHriZen, you code doesn't work. The username is OK, but the error message comes up automatically as the password.
 
As a c++ newbie myself, I like Wilco's code the best. It makes the most sense to me. It's not overly complicated and it gets the job done nicely.
 
Anable, Wilco's code doesn't work in the same way as the others, some, let you enter a character based username :)
 
I don't want numbers as the username and password. I want words. I am trying to use 'char' to do that. Wilco's code works fine for me.
 
Yeah... use either char *variablename or char variablename[int]

Then just use strcmp to compare them.
 
what does char *variablename do? Does it create an array with no pre-defined number of elements?
 
Your confusing me. I only got up to Cunebelin's(spelling?) 3rd tutorial. I want to do some thing like:
PHP:
#include <iostream.h>

int main()
{
    int z;
    char a, b;
    cout<<"Username: ";
    cin a;
    if (a == 'Dildo')
        cout<<"Password: ";
    cin b;
    if (b == "Ugly")
        cout<<"Welcome Dildo!";
    else
        cout<<"Icorrect username or password. Please trygain.";
    return 0;
I don't know if that will work. I haven't tried yet. :D
 
Ray_man in C++ you make an array of chars to make a string or i think you can use one of the standard C++ libs to make a string more easily.

PHP:
char stringName [] = "hello";
char stringName [] = {'h','e','l','l','o'};
char stringName [5];

cin.getline(stringName, 5);

refere to sandmans post on how to compare them.

EDIT: learn how to use arrays first if you havn't.
 
ray_MAN said:
EvNTHriZen, you code doesn't work. The username is OK, but the error message comes up automatically as the password.

Really? what do you mean? what is it doing? i didn't understand what you had said...
 
mrchimp said:
Ray_man in C++ you make an array of chars to make a string or i think you can use one of the standard C++ libs to make a string more easily.

PHP:
char stringName [] = "hello";
char stringName [] = {'h','e','l','l','o'};
char stringName [5];

cin.getline(stringName, 5);

refere to sandmans post on how to compare them.

EDIT: learn how to use arrays first if you havn't.

The standard library is needed to use strcmp. In borland it's stdio.h, in msvc it's stdlib.h. Syntax is as follows: strcmp(const char*, const char*);
you may need to typecast if you use a "string" variable from string.h. To change that into a c string, you would say: mystringvariable.c_str() for every instance of that string that you want to convert to a c string.
 
Fun STL Vectors

The more C++ features you can use to your advatage, the better.

HL2 and it's SDK for mods will be using C++, so lots of classes and standards will be used.

This code is a little sloppy and could be done cleaner, but I didn't want to make it hard for some of you beginners to understand (although I'm still kind of a beginner).

I really recommend the book: The Waite Group's C++ Primer Plus. Although www.cplusplus.com is free, and is a great for newbs and for reference once you get good. :dork:



PHP:
#include "iostream"
#include "vector"
#include "string"

using namespace std;

class user
{
private:
	int id;
	int password;
	string name;
public:
	user(int _id, int _password,string _name)
	{
		id = _id;
		name = _name;
		password = _password;
	}

	int getID()
	{
		return id;
	}

	int getPassword()
	{
		return password;
	}

	string getName()
	{
		return name;
	}
};

int main()
{
	bool access = false;
	int id;	
	int password;

	vector <user> database;
	user gordon = user(201, 5291,"Freeman");

	database.push_back(gordon);


	cout << "Enter your user ID: ";
	cin >> id;

	cout << "Enter your password: ";
	cin >> password;

	for (int i = 0; i < database.size(); i++)
	{
		if ((id == database[i].getID()) && (password == database[i].getPassword()))
		{
			access = true;
			cout << endl << "Welcome Dr. " << database[i].getName().c_str() << endl;
		}
	}

	if (access == true)
	{
		cout << "Black Mesa Research Facility:"<< endl;
		cout << "=============================" << endl;
		cout << "1. Menu" << endl;
		cout << "2. Goes" << endl;
		cout << "3. Here :)" << endl << endl;
	}
	else
	{
		cout << "Access Denied" << endl;
	}
	
	
	return 0;
}
 
Yes, I am buying C++ Primer Plus. I already had it on hold. And I will post a pic of the code that doesn't work. OK? I might not though. Just got UT2004! :bounce: :bounce:
 
I know its only his first program. I wasnt pointing a coding error, just a logic error. It was just in case he had the logic of if statements and code blocks confused. You need to be able to understand the code and see what its doing :) If it was overly complicated then ignore it, youll pick it up as you write more stuff.
 
I just finished a course at my High school where we learned C++.
Here is the final project that myself and friend wrote:

DOWNLOAD INSTALLER

I realize that it is bad, there are quite a few bugs, but it is playable...enjoy!
 
Sorry for the wait, I just got addicted to UT2004. I took a screenshot of the problem.
 
well for a start im wondering where that "Password:" output comes from, because its not in your code anywhere. MSVC++ doesnt seem to use string class so I cant comment on the rest of it. (usually you have to use strcmp(string1,string2) to check if strings are right.
 
Yeah, I just noticed that. I am waiting for EvNTHriZen's reply, for he is the author of that code.
 
ok.. check this out... i see what you are doing wrong. In this code you need to input the user name followed by a space then the password. so the input from YOU would be

RAY MAN

The first word is the user name, the second is the password. You only inputted RAY. try this:
PHP:
cout<<"Username:";
cin>>Name;
cout<<"Password:";
cin>>password;
//rest of code same.

It is more explicit like this. I only did it the other way to give you an example.
 
No, it didn't work. This is freaky. Even when I omit:
PHP:
else
    {
      cout<<"Incorrect username or password. Please hit F9 to try again.";
    }
I still get the same response when I am asked to type the password:

Username: Ray
Password: -=ERROR=-Incorrect username or password! Please hit F9 to try again.

Very wierd.
 
I just compiled EvNTHriZen's original program, didn't change a letter. Just pasted and compiled. Works perfectly for me..
 
Try doing a complete rebuild... I don't know how to do it in Borland, though... if you are changing code, and it isn't coming in the build as different, then the full rebuild might work.. There really isn't anythig special that i am aware of with this code...

But anywho... you get the idea on how to read in a string easily.. it can get more complicated then that, but this works for now.
 
This is wierd. I can't get the proper results in a project with 2 files. I had to create a new project to run the code properly. That sucks. I need money for VC++. Anyway, thanks for the help guys I appreciate it. Does anyone happen to know the price for VC++?
 
Um.. it's pretty expensive. You are using Borland, right? check to see if there is a way to have teh compiler delete all filesit has created and start a new compile for that project. In VC++ it's called "Rebuild All".

I'm glad to see it wirked.
 
ray_Man,

what do you have in those three open(1,2,3).cpp files? do you have more than one main() being compiled across those files somehow?

maybe just copy the code to a new project. if there is more than one instance of main() in a project (even in separate cpp files) there might be a conflict.
 
Back
Top