Like Tree5Likes
  • 5 Post By MK27

My first project (:

This is a discussion on My first project (: within the C++ Programming forums, part of the General Programming Boards category; Greetings everybody! This is my first post, so hello to you all reading this I am a new programmer, as ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    My first project (:

    Greetings everybody!

    This is my first post, so hello to you all reading this
    I am a new programmer, as a hobby at the moment but I am hoping I get the hang of it and get into the field I want (Learning this language is exhausting me!)

    I figured the best way to put a test to my knowledge of what I have learned so far of the tuts on this websites would be to try and create a program. Maybe this isn't the best idea but who knows, I might be able to get it to work

    The trouble I am having is the first language I designed in was when I was 12 - 14 and I was using Visual Basic, which was really easy because all you had to do was put the code into pre-defined sections (Like double click the button to edit the code that will execute if it is pressed) but recently I have noticed that, it isn't always that simple!

    Wrapping my head around code is one thing, but then figuring out where it will all go is the next, and I am not to sure that this code is going to be efficient in what I want it to do!

    My aim is to have the program search, edit, and create files (Btw I am using windows xp and don't have a compiler because I am using Linux MINT as my main OS but it just crashed on me and I lost all my code so I booted windows lol)

    I know this code is FAR from even slightly completed, but could you please tell me if I am on the right tract and what I am and what I am not doing correctly? I would be very grateful! (:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    // Function calls
        int Search_for_files ( int user_input, int user_input2 );
        
    // Function Definitions
        int Search_for_files ( int user_input, int user_input2 )
        {
            int loop_breaker;
            for ( int loop_breaker; loop_breaker = 0; )                                             // Calls the loop so the program will not close on an invalid input
            {                                             
            switch ( user_input )                                                                   // Calls the switch function
            
            case 1:
            cout << "You have selected to search for files on the Hard Disk Drive." << endl;        // Tells the user what he/she has selected
            loop_breaker = 0;                                                                       // This will break the loop, and allow the execution of other code.
            break;                                                                                  // Breaks out of the case so the program does not fall through
            
            case 2: 
            cout << "You have selected to go back!" << endl;  
            loop_breaker = 1;
            break;
            
            }
        }
    
    
    int main()
    {
        
         int Selection_1;
        cout << "Please select an option from the list below:" << endl;                            // Tells the user he/she needs to make an option with the following as his/her choices
        cout << " " << endl;                                                                       // Put some space between the previous text :) 
        cout << "1. Search for files" << endl;                                                     
        cout << "2. Edit a file" << endl;
        cout << "3. Create a new file" << endl;
        cin.ignore();    
        
        for ( int loop_breaker1; loop_breaker1 = 0;)
        {
            
        cout << "Selection: ";
        cin >> Selection_1;
        
            switch ( Selection_1 )
            
            case 1:
            cout << "Search for files selected." << endl;
            loop_breaker1 = 1;
            break;
            
            case 2:
            cout << "Edit a file selected." << endl;
            loop_breaker1 = 1;
            break;
            
            case 3:
            cout << "Create a new file selected." << endl;
            loop_breaker1 = 1;
            break;
            
            default:
            cout << "Invaild Selection. Please re-try, or enter 3 to close the program." << endl;
            loop_breaker1 = 0;
            break;
            
        }
    
        
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    You need a compiler to code effectively. Test Drive Comeau C/C++ Online

    Your loop-breaking system seems interesting. How does it work?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by CodeMonkey
    Your loop-breaking system seems interesting. How does it work?
    Good catch

    Quote Originally Posted by xScen3xHdstyl3x
    I know this code is FAR from even slightly completed, but could you please tell me if I am on the right tract and what I am and what I am not doing correctly?
    Answer CodeMonkey's question. In particular, consider the difference between = and ==. Also, consider that variables should be initialised, and that there is a bool (boolean) type in C++. Is a for loop really appropriate? Have you considered a while loop?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by xScen3xHdstyl3x View Post
    I know this code is FAR from even slightly completed, but could you please tell me if I am on the right tract and what I am and what I am not doing correctly?
    The most serious thing you are doing wrong is: NOT TESTING AS YOU GO!

    Do not write a whole program and then try to compile it. At this point, you should try to compile your code every 5-10 lines or 15 minutes, whichever comes first. That may mean you have to add a line or two temporarily to have it make syntactical sense (eg, if you are working on a loop, you may have to use a temporary condition if the condition you are considering is not ready to evaluate. Etc.). The program does not have to do anything, but it must compile successfully. Make sure to enable your compiler warnings. Then -- if it doesn't do anything -- run the executable to make sure you don't have any unpredicted problems at runtime (such as segmentation faults).

    Quote Originally Posted by xScen3xHdstyl3x View Post
    (Btw I am using windows xp and don't have a compiler because
    This is not a good excuse. If you've used gcc on linux, you can download MinGW, a version for windows. It might take you 1/2 hour to get it working.

    If you don't have access to a compiler, I strongly suggest you find something else to do in the meantime. Without being able to test, you will be prone to repeating the same error over and over again. For example:

    Code:
            switch ( user_input )                                                                   // Calls the switch function
            
            case 1:
            cout << "You have selected to search for files on the Hard Disk Drive." << endl;        // Tells the user what he/she has selected
            loop_breaker = 0;                                                                       // This will break the loop, and allow the execution of other code.
            break;                                                                                  // Breaks out of the case so the program does not fall through
            
            case 2:
    This will cause a compiler error, because the "case" statements are supposed to be inside a block:

    Code:
    switch (whatever) {
    	case 1:
    	case 2:
    	default:
    }
    You did that BOTH times you used a switch statement. Not such a big deal to fix, but you could just as easily have made some other syntax mistake repeatedly with something else that would require more work.

    More serious potential time wasters than syntax or usage errors when you don't test your code are errors in logic which would show up if you compiled and ran:

    Code:
        for ( int loop_breaker1; loop_breaker1 = 0;)
        {
    "Loop_breaker" is uninitialized, therefore almost certainly not 0, so presuming you meant "==" there, your loop will break before it runs even once. And if you meant "=", it will never not break. Two cascading errors in one line!

    Again, not so serious, but it could just as easily have been something that would require you to rewrite a bunch of code -- because you thought about one thing the wrong way. The process of compiling, testing, and correcting also forces you to focus more on your code. Eg, the use of the "loop_breaker" variable is superfluous to the use of the "Selection_1" variable. You'll be able to simplify the code if you remove one or the other.
    Last edited by MK27; 09-26-2011 at 09:01 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    Smile

    The most serious thing you are doing wrong is: NOT TESTING AS YOU GO!
    Yeah I know, I was only doing this temporarily as I got annoyed with Linux, and I am since paying for it lol. I will definitely pull my head in and start compiling as I go. Assuming my code will work regardless is probably the most rookie mistake you can make..

    That may mean you have to add a line or two temporarily to have it make syntactical sense (eg, if you are working on a loop, you may have to use a temporary condition if the condition you are considering is not ready to evaluate. Etc.). The program does not have to do anything, but it must compile successfully.
    That makes a lot of sense, I will definitely remember that one Thank you sir!

    This is not a good excuse. If you've used gcc on linux, you can download MinGW, a version for windows. It might take you 1/2 hour to get it working.
    The idea of working on Windows repulses me. It took me 2 minutes to set up Code::Blocks on Linux and I'm not looking back lol.
    I also fixed up the case errors (Not being in blocks) I found that out because before I went to bed I booted linux and attempted to compile, and I started realizing what I have been doing wrong.

    Your loop-breaking system seems interesting. How does it work?
    The basic idea was if the code was executed in one of the Cases (Such as 1 || 2) the code would break out of the loop and would be able to execute different code, but if the code reached default, it would continue the loop by registering the condition of loop_breaker to equal true, thus resetting the loop until a valid input from the user was selected. Now looking at it, I'm also not fully understanding why I didn't use while or even and if block. ~.0

    Thanks for the really really constructive criticism on the code, I can tell this isn't going to be as far fetched as I thought with such a great community helping me out! (I do however promise not to constantly ask annoying redundant questions :P lol )

    But there is one more thing, I don't know if I am creating my Functions properly, because when I call them, I get an error saying
    'Error: Expected primary expression before 'int''
    This is really confusing me, I don't know if I've gotten the concept of Functions all wrong entirely or if it's some silly little mistake?! (I know I know, probably should have seen if it compiled in the first place, right? haha.)

    Thanks again guys (:

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Well, at the moment, you have no function call and a lot of code that won't compile. You will have to give us some context.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    Smile

    I completely restructured the code now. I figured the old code was horrible and redundant in most parts, so here is the new
    This compiles and runs well, however I am still working on it.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    
    void Search_for_files()                                                                                 // Create a Function of type VOID for when the user wants the seach menu.
    {
        int Selection;
        system("cls");
        cout << "                      _-_-_-_Search Menu Index_-_-_-_" << endl;
        cout << " " << endl;
        cout << "Please make a selection from the following options to continue: " << endl;
        cout << "1. Search for Files on the Hard Disk Drive." << endl;
        cout << "2. Search for Files on an External Media Device." << endl;
        cout << "3. Go back. (Previous Menu)" << endl;
        cout << "Selection: ";
        cin >> Selection;
        cin.ignore();
    
    
        if ( Selection == 1 )
        {
            cout << "You have selected to Search for Files on the Hard Disk Drive." << endl;
        }
        else if ( Selection == 2 )
        {
            cout << "You have selected to Search for Files on an External Media Device." << endl;
        }
        else if ( Selection == 3 )
        {
            cout << "You have selected to Go Back. (Previous Menu)" << endl;
        }
        else
        {
            cout << " " << endl;
            cout << "               You have not entered a valid input. Please try again." << endl;
            Search_for_files();
        }
    }
    
    void Edit_files()
    {
        char path_string[51];
        cout << " " << endl;
        cout << "                      _-_-_-_Edit Menu Index_-_-_-_" << endl;
        cout << " " << endl;
        cout << "Please enter the location (path) of the file that you would like to edit." << endl;
        cout << "Path: ";
        cin.getline ( path_string, 51, '\n' );
        cout << "Searching for file @ Path: " << path_string << endl;
        system("edit");
        path_string;
        cin.get();
    
    }
    
    int main2();
    int main2()                                                                                             // This will create a complete function that acts just like main, but can be
    {                                                                                                       // Re-Called at any given time.
        int continue1;
        cout << "Welcome to the File Search and Manipulator Program." << endl;
        cout << " " << endl;
        cout << "Please make a selection from the following options to continue: " << endl;
        cout << "1. Search for files" << endl;
        cout << "2. Edit Files" << endl;
        cout << "3. Create a File" << endl;
        cout << "4. Terminate Program (Close)" << endl;
        cout << "Selection: ";
        cin >> continue1;
        cin.ignore();
    
        switch ( continue1 )
        {
            case 1:
            cout << "You have Selected to enter the Search Menu." << endl;
            cout << " " << endl;
            Search_for_files();
            break;
    
            case 2:
            cout << "You have Selected to enter the Edit Menu." << endl;
            cout << " " << endl;
            Edit_files();
            break;
    
            case 3:
            cout << "You have selected to Create a file." << endl;
            cout << " " << endl;
            break;
    
            default:
            cout << "You have not entered a valid response. Program will now Terminate." << endl;
            cout << " " << endl;
            break;
    
        }
    }
    
    int main()                                                                                              // Call the main function.
    {
        main2();
    }
    All of this code is working as desired, (I thought I would post for reference if anyone else was perusing the same outcome)

    But there is one thing I am worried about working on, and that is having the program search for a file on the hard drive. How would I go about doing this? and there is one problem with the code, if you
    enter a char type into any of the selection: requests, the loop continues to go haywire because it sets the var to a char and it just continually loops and you cannot stop it. So is there any way you can possibly clear a variables value? I've searched all over the net and all I can find is how to delete the values in arrays..

    And one more thing, I was fiddling with this earlier (And soon realized what I was trying to do was not necessary, but it did bring up rather a good question):
    How do you stick characters to a string of text or even numbers to an integer?
    I was using the system() command, and I was wondering (Because I knew I could do system("cd") but not system("cd" (Path_String))
    So how would you attach pre-defined characters to a users input?
    An example, how would you attach CD to a user inputted string reading C:\ so it would read "CD C:\"? There must be a way to do this?

    Thanks in advance Craig.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    PS, If the int main2() isn't self explanatory, I used that because I cannot seem to directly recall int main()..

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You should never call main(). If you want to loop the code in main then I suggest you use loops. As for the rest of your code, you really should sit down and bother to learn how to program in C++. I would suggest you get a good book, in the mean time our C++ Made Easy Tutorials are a good starting point. Using system calls is not programming, you are essentially writing a batch script in C++; this technique is filled with potential pitfalls and is error prone. For the things you want to do, you have left standard c++ and have entered into the realm of OS specific API calls.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    Quote Originally Posted by AndrewHunter View Post
    As for the rest of your code, you really should sit down and bother to learn how to program in C++.
    I think that was a little arrogant to say the least, but I'll ignore it as I know I am a beginner, and just remember we were all beginners once.

    Quote Originally Posted by AndrewHunter View Post
    Using system calls is not programming, you are essentially writing a batch script in C++; this technique is filled with potential pitfalls and is error prone.
    I didn't think it was, and I was only using it as basis to get it working the way I wanted it in a nutshell so I could further more go out and learn more about the C++ methods of doing what I am trying to achieve, so thank-you again for your 'constructive criticism'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes Project (Not class project)
    By adam.morin in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2011, 01:48 AM
  2. Paid project - The Free Marketing Project
    By sharefree in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-27-2010, 03:15 PM
  3. RTS Project
    By strokebow in forum Game Programming
    Replies: 14
    Last Post: 10-05-2008, 11:46 AM
  4. AI project
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2002, 01:23 PM
  5. your 11th & 12th grade c++ project or similar project
    By sleepyheadtony in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-13-2002, 05:14 PM

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