Hello, l sor of scribbled this code up from a few tutorial l have looked over, combining tutorials never works for a newbie to C++ :( now this is what l have written::

// InfoMenu.cpp : main project file.

#include <stdafx.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  int loop=1;
  int choice;
  string getinput;

  while(loop==1)
{
	cout << "Type 'exit' to leave at any time\n"
		 << "Type 'login' to begin\n";
    cin >> getinput;
    if(getinput=="exit") // Exit Code
	{
		exit(0);
	}
	if(getinput=="login") // Login Code
	cout << "Username:"; // Username Code
	cin >> getinput;
	if (getinput=="Test")
	{
		cout << "Passsword:"; // Password Code
		cin >> getinput;
		if(getinput=="Pass")
        cout << "Welcome...";
	  	
	}
  }
}

It is just a menu thing, pretty much first thing l have ever built. Here is what l would like but l dont really have and know how to do.
1. l have an exit code, but this only works in the main menu where you are prompted to type exit or login, how can l get this to work in every writing bit?
2. Secondly l would like to have two different accounts, how can l do this?
3. How can l add a commend after each prompt that if it is not the same as the the (getinput=="~~~") it goes back to the start?
sorry for all the questions but l am sort of confused and lost here...

Recommended Answers

All 21 Replies

3. How can l add a commend after each prompt that if it is not the same as the the (getinput=="~~~") it goes back to the start?

Can you clarify what you mean by this?

Can you help me at all? what l mean is, can lk add a command that goes back to the start of the script if the input is not equal to what it should be, so for Username, if it is ot Test, l want it to go back o the start.. Is this possible?

You can do it but it will require a lot of nested if statements. You could either have a command on each level and have the user only exit one level at a time --> e.g., login -> choose menu item 1 ->into menu 2->exit->back to first menu->exit->logged out -or- you could just call exit(0) whenever the user types in exit. The latter may become more error prone.
Is "if" statements the only thing you have covered so far? Anything with arrays or functions? Those two things might make this slightly easier.

You can do it but it will require a lot of nested if statements. You could either have a command on each level and have the user only exit one level at a time --> e.g., login -> choose menu item 1 ->into menu 2->exit->back to first menu->exit->logged out -or- you could just call exit(0) whenever the user types in exit. The latter may become more error prone.
Is "if" statements the only thing you have covered so far? Anything with arrays or functions? Those two things might make this slightly easier.

ahaha, well now you mention it, no, l haven't really learnt arrays or functions.... Should l? Or do you know of a basic menu anywhere that has these ideas incorporated and l could just learn from that?

If you're wanting to learn something directly applicable, read about switches. It's almost like a shortcut to writing systems of nested if else statements. Check out:http://www.fredosaurus.com/notes-cpp/statements/switch.html
I suggested the arrays because it would be nice to store your usernames in one array and passwords in another so you can compare usernames to passwords easily.
I suggested functions because it might be nice to have a function to govern the login so that you're not dealing with the password verification stuff in the main function.
Neither of the two things above is really required, though.
So you're really free to do however much you want with it. If it is one of your first few projects, keep it reasonably simple. Also, it helps to make a piece of it first, test that out then add another piece.

If you're wanting to learn something directly applicable, read about switches. It's almost like a shortcut to writing systems of nested if else statements. Check out:http://www.fredosaurus.com/notes-cpp/statements/switch.html
I suggested the arrays because it would be nice to store your usernames in one array and passwords in another so you can compare usernames to passwords easily.
I suggested functions because it might be nice to have a function to govern the login so that you're not dealing with the password verification stuff in the main function.
Neither of the two things above is really required, though.
So you're really free to do however much you want with it. If it is one of your first few projects, keep it reasonably simple. Also, it helps to make a piece of it first, test that out then add another piece.

Woh, thanks you have been extremely helpful :) l will look into all three of these, also, do you know of a place, tutorials, etc that could teach me to make my program LOOK different, l mean the standard program is sort of start run sort of stuff, l want to be able to give it that blue bar and stuff, if that is possible :) thanks againn..

Woh, thanks you have been extremely helpful

Hey, no problem at all. Definitely post back with further questions on this.

Are you talking about making a program in a window instead of the console? You'll probably want to polish your basics a bit but look at http://www.winprog.org/tutorial/ for Win32 (MS has other window frameworks but Win32 is the most fundamental). There are other toolkits like Qt and wxWidgets that can be used with other compilers.

Hey, no problem at all. Definitely post back with further questions on this.

Are you talking about making a program in a window instead of the console? You'll probably want to polish your basics a bit but look at http://www.winprog.org/tutorial/ for Win32 (MS has other window frameworks but Win32 is the most fundamental). There are other toolkits like Qt and wxWidgets that can be used with other compilers.

Ok than l think this all l need for now, thanks a lot for this you have been very helpfull...
EDIT: yes l did meen making it in a window instead of a console...

Actually, l am using Visual C++ and in File, New, Project, Win32 and then Win32 Project, it already has a basic program, none of it makes much sense to me but, so l have no idea how to edit it...

Select Win32 Console App (and while at that uncheck use precompiled headers and leave empty project unchecked). That should get you started with it. I don't know if you can change it after it's been created, just start a new one.

Select Win32 Console App (and while at that uncheck use precompiled headers and leave empty project unchecked). That should get you started with it. I don't know if you can change it after it's been created, just start a new one.

So the code l start there Jonsca is abit like this then??

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
    return 0;
}

Got it straight from http://www.winprog.org/tutorial/start.html Are they sort of the same thing, cause l just copied and pasted over what was put there by default and it came up with and error message :(

Not for the console app (which is the best setup for programs like you had initially), but for the Win32 program setup that's exactly the type of thing you need. Console app has a main() function.
There are arguments either way but it's usually easier to get the language down pat with the console stuff first...

Not for the console app (which is the best setup for programs like you had initially), but for the Win32 program setup that's exactly the type of thing you need. Console app has a main() function.
There are arguments either way but it's usually easier to get the language down pat with the console stuff first...

Oh ok then thanks again :)

I'm have not well understood but I thinks functions are good ways to go.
Here I have little time (at work) so I will try to do little thing to show example of function. One can improve it ;)

// InfoMenu.cpp : main project file.

#include <stdafx.h>
#include <iostream>
#include <string>

using namespace std;

//define function prototypes
void MyMenu();

int main()
{
int test=1;//test conditio that user want to continue{1=Yes, 0=no}
if(test==1){
	MyMenu();
}
else{
	return 0;
}
  
}

void MyMenu(){
	
	  int loop=1;
	  int choice;
	  string getinput;

	  while(loop==1)
	{
		cout << "Type 'exit' to leave at any time\n"
			 << "Type 'login' to begin\n";
		cin >> getinput;
		if(getinput=="exit") // Exit Code
		{
			exit(0);
		}
		if(getinput=="login") // Login Code
		cout << "Username:"; // Username Code
		cin >> getinput;
		if (getinput=="Test")
		{
			cout << "Passsword:"; // Password Code
			cin >> getinput;
			if(getinput=="Pass")
			cout << "Welcome...";
			
		}
	}
  }
	
}

Just curious, why do you use #include <stdafx.h> in C++?

@evsteve Check out Precompiled headers

combining tutorials never works for a newbie to C++

Newbie to C++ trying to use WinAPI is going to result in a lot of headaches and errors. I agree with sticking to a console app.

Newbie to C++ trying to use WinAPI is going to result in a lot of headaches and errors. I agree with sticking to a console app.

l can't eveen find tutorials on building programs, just console apps... Is the code used to build a console app even the same as the code to build a program? A program being what is used on computers the most...

Probably more than 3/4 (just a guesstimate) of any Windows programming requires non-standard extensions to C++. So trying to learn C++ that way would be like trying to derive the rules of grammar from a (hypothetical) variant of English that's highly dialected and loaded with slang.

That being said, there are tutorials out there. Available via Creative Commons is C++ GUI Programming Using Qt4 (here) that I would say is "accessible" to a beginner but contains many idioms within the code to drive the process of GUI building (their own macros for example).

And of course there's Win32 and MFC (MFC not being available on the Express Editions of the Visual C++ compiler) as well as CLR/Winforms (which really messes with the C++ syntax and makes it more like C# for .NET integration).

I'm sure I've omitted some possibilities but my overall message is, take some time now, learn the language (and it's quirks and gotchas). Then, look into a book like this which I've honestly never read but people speak very highly of it (there's a 2010 edition coming out at some point). Check out the books sticky thread on the forum but I've forgotten if there are any GUI books there.

And why are you claiming console apps are not programs? How do these definitions not fit a console app? :icon_wink:

And why are you claiming console apps are not programs? How do these definitions not fit a console app? :icon_wink:

Don't worry, you have completely missed the point...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.