Thread: C++ Console program help

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Question C++ Console program help

    I am a new programmer, and I have a couple of questions.

    Before I ask, im sorry if its been done before, but im at a loss as to what to search for, as i have no idea how to even begin what id like to do.

    Here is my code...

    Code:
    #include <iostream>
    #include <string.h>
    #include <string>
    #include <dos.h>
    #include <process.h>
    #include <fstream>   // for files
    #include <stdio.h>
    #include <conio.h>
    
    
     
    using namespace std;
    
    
    string user;
    string pass;
    int main()
    {
    
    	cout << "Username :";
    
    	cin >> user;
    	if(user == "admin")
    	{
    		cout << "Password :";
    	}
    	else
    	{	
    		cout << "Incorrect username\n";
    		return main();
    	}
    	cin>> pass;
    
    	if(pass == "12345")
    	{
          cout<< "Welcome.\n";
    		
    	}
    	else 
    	{
    		return main();
    	}
    
    
    	system("pause");
    
    	return 0;
    
    }

    After the console displays "welcome" id like it to allow the user to "press any key to continue..." ( getch;? ) to clear the screen.

    After the screen clears, id like the user to be able to type in text that can be saved within the program, not an outside text file, so the text can only be accessed through logging in.

    This will serve as a personal journal if you will.

    How do i go about doing this?

    Thank you guys.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot save text inside your program and expect it to remain there. Impossible.
    It will have to be saved to some outside file. No way around that.
    So what do other applications do? They encrypt the file. Only knowing the correct password can decrypt it and read the data.

    Also, it is bad to recursively call main. You should use a loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Question

    Quote Originally Posted by Elysia View Post
    You cannot save text inside your program and expect it to remain there. Impossible.
    It will have to be saved to some outside file. No way around that.
    So what do other applications do? They encrypt the file. Only knowing the correct password can decrypt it and read the data.

    Also, it is bad to recursively call main. You should use a loop.


    I knew that i should use a loop, but i am unsure how to use it. As far as encrypting data, how do i use that in my program, Then how do i even allow the user to type freely in the console, and then save it to an encrypting file?

    What tutorials should i look up that will explain how to do these and put them inside my program?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about the typical "while the answer is incorrect, ask for username"?
    The point is that you input the data, encrypt it, write it to file. Then you read the data, decrypt it, and print it. I'd suggest a search on google about encryption. There is bound to be lots of information. Both easy and hard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Code:
    #include <iostream>
    #include <string.h>
    #include <string>
    #include <dos.h>
    #include <process.h>
    #include <fstream>   // for files
    #include <stdio.h>
    #include <conio.h>
    
    
     
    using namespace std;
    
    
    string user;
    string pass;
    int main()
    {
    
    	cout << "Username :";
    
    	cin >> user;
    	while(user != "admin")
    	{
    		cout << "Incorrect user name :";
    	}
    	do
    	{	
    		cout << "password\n";
    		return main();
    	}
    	cin>> pass;
    
    	while(pass == "12345")
    	{
          cout<< "Welcome.\n";
    		
    	}
    	do 
    	{
    		return main();
    	}
    
    
    	system("pause");
    
    	return 0;
    
    }
    Is that anywhere near correct?


    Now that i know to just google c++ encryption, what about XOR encryption for the simplicity of this program

    What about writing to a console as a user note pad?

    how do i start that?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    cin >> user;
    while(user != "admin")
    {
    	cout << "Incorrect user name :";
    }
    that's an infinite loop. Any time you have a loop, the condition you're testing - in this case user != "admin" - must have a chance of becoming true or it will go on forever. Because the user variable does not change inside the loop, how can you get out?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think you understand how loops work! There is no while ... do loop. Might I suggest you look up what loops are available and how they work?
    Once you get the syntax down, it's just pure logic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    31

    Lightbulb A different approach

    I don't know about the rest of you, but I've never seen a while..do loop before. Apparently my compiler hasn't either. Am I just 'out of the loop'? Ha ha.

    If you are looking to produce a password-protected text editor, well, you're not going to be churning out any Notepads or VIMs any time soon. With a basic knowledge of C++, you will virtually only be able to--with any ease--append to a file. Editing what's already written will be murder. Most likely not what you want.

    So my suggestion to you would be this: if you read the tutorial I mention below, and a good one on file encryption, you will be ready to build a file encrypter/decrypter. You could use your favorite text editor to edit the journal/whatever, and when finished editing, run it through the file encrypter to secure it. When you want to edit it again, run it through your decrypter to make it accessible. You could easily make your decrypter password-protected. This approach would take SIGNIFICANTLY less code and knowledge of the language.

    I recommend looking at this site here, which takes a pretty wicked look at more I/O functionality. It goes way more in-depth than any other web tutorial I've seen. I can't supply you with a tutorial on encryption, but maybe someone else or Google could.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  3. Intergrating console program into windowed app
    By spadez in forum C Programming
    Replies: 4
    Last Post: 02-26-2009, 12:58 PM
  4. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  5. How to avoid console program crash when terminated too early
    By Xargo in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2007, 04:43 PM