Low Priority: How am I coding?

// I recently have been doing a lot more reading over actual coding, so I made this little simple unfinished-but-working program. Since I have not yet really posted any code yet, I was just wondering if anyone there was interested in seeing my current style, because I would LOVE to know if you have any better alternatives to things, or better habits, or just things I should know. I didn't add comments also because I thought it might sort of be self-documenting, in fact I think you will realize where I'm going with this... tell me if I'm wrong!(2nd issue below code):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;

int main()
{
    int imNint = 0;
    char nameU[21] = "YOU";
    char anderson[6] = " AND ";
    while (imNint <= 3)
    {
        cout << nameU << " ARE ";
        while (imNint == 0)
        {
            cout << "silly" << anderson;
            imNint++;
            continue;
        }
        while (imNint == 1)
        {
            cout << "a loser" << anderson;
            imNint++;
            continue;
        }
        while (imNint == 2)
        {
            cout << "fluffy" << anderson;
            imNint++;
            continue;
        }
        while (imNint == 3)
        {
            cout << "derp" << anderson;
            imNint++;
            continue;
        }
    }

    if (imNint >= 4)
    {
        cout << "I'm done talking about " << nameU << "..." << endl;
        cout << "Give me a name that's 20 characters max.\n";
        cout << "If it goes over that limit I WILL END THIS PROGRAM >.>!\nEnter name: ";
        cin >> nameU;
    }

    return 0;
}


// So basically after you enter a name, I plan on making it repeat what it said with the name you entered. I decided that if you enter a name over 20 characters it would end the program, both a safety feature and an exit. I think I can figure this all out, and I'm about to use those "search bars" to find out how to check to see if that char array(that is an array, right?) nameU can be checked for how many characters have been entered. Off to search!
I find this programming structure poor, i am from beginner to intermediate and i think you have too many while statements, maybe use the switch statement which is more better for this kind of situation and what kinds of papers do you look at, beginner - expert? Also i can try to help fix your program for you and i will post it later or another day XD.
This is C/C++, not Java. You need to review C strings/arrays and then take a second look at Lines 7 and 8.
// I hope that you notice that the goal of this program is not focused on an 'end result', this is just me programming "something".. as in "practice makes perfect". Don't be looking at this too seriously! These are the first while loops I've basically done other than from copying code from a book as it says.

// I know nothing other than C++, not even BASIC... so that "This is C/C++, not Java" doesn't help xP. What's this about "C" strings/arrays? I would rather stick to C++ over any C code unless really necessary, I hear too many debates about people using C code just because they're used to it instead of using the supposedly better C++ alternative.

// Please remember that the things I do are for educational reasons and I'm not just trying to "get from point A to point B". I like seeing all the alternatives. Still awaiting any comments.
Are you looking for comments on coding style or on program logic? Style--things like brace placement and
other formatting--is subjective. You're not doing anything there that is egregious.

As far as program logic goes, it is hard to understand. kfmfe04's comment was in regards to the size of
the arrays vs. the number of characters you put in the array. The array need only be as big as the string
to fit in it. Although I see that while you are initializing with only 4 characters, you later read into it.
However, char arrays are C, not C++. The C++ way of dealing with strings is the std::string class.

1
2
3
4
5
6
       while (imNint == 0)
        {
            cout << "silly" << anderson;
            imNint++;
            continue;
        }


This loop will execute exactly once, every time. A loop that executes exactly once every time is not a loop.
It could be reduced to cout << "silly" << anderson; Same for the subsequent loops. The if()
check on 38 will always be true, so it is pointless to even check. The while loop on 9 will execute exactly
once, so it can be reduced.
And then there's the fact that the last comment is false.

You can insert a name that's one character long and it will still terminate.
You should try to program using good techniques, not bad ones and your using the while loop for the wrong purpose, that uses quite a bit of memory. What you actually want to do is minimize as much memory as possible when creating you programs and you should really learn C before C++ since C is the basic foundation of C++, the only things that have changed are things like cout which in c is printf and many more.
I recreated your program and hopefully you can learn something from it =). Enjoy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
using namespace std;

int main() // Program starts here
{
    char Name[15] = " ";
    int repeat = 0; // Counter
    char q;
    
    cout << "\n  You are a silly loser with fluffy ears and a very big derp\n\n";
    
    cout << "  I'm done talking about you...\n\n"
         << "  Give me a name that's 15 characters max.\n"
         << "  If you type q, I WILL TERMINATE YOUR COMPUTER >.>!\n\n";
            Repeat:
    cout << "\n  Enter Name (q): ";
    
    
    cin >> Name; // Asks user to type a name
    
    while (repeat != 5)
    {
          cout << "\n  I love (Hate) " << Name << " ^^\n";
           
          repeat++; // Increases repeat by 1
    }
    
    goto Repeat;
    
    return 0;
}
woops, forgot something, this is the latest one, lolz, forgot to add q this last one =P. Fixing the bugs i have, lolz. I better do more of this programming was very slack for the past half year xd.
Sorry, can't really be bothered to help, sorry. I need to relearn about arrays and pointers again coz of the slackness, i didn't do programming 4 nearly a year, lol. Anyway u can use my first program and recreate it, gl =)
@magik994

Using goto is not good style in c++ programming either
Yea, Goto is leftover from the linear programming days and doesn't really have a place in OOP or event/procedural languanges. In VB we were forced to use it for error handling because it doesn't have try/catch, but for C/C++ I can't think of any reason we would ever include it.
Funny how language committees keep goto in the language while armchair hackers debate its usefullness.

Also, it is not necessary to learn C to learn C++. I actually recommend against it because then you won't have to unlearn bad habits.

The loop that magik994 uses is off in a couple of ways: 1) the loop variable is initialized somewhere other than where it is used, and 2) typically in counting loops you use < instead of !=, as this makes your intent clear. Also, your variable names stink: you've got one declared that is not used ('q'), and you seem to have case-infatuation -- your loop variable is named exactly the same as a goto label, except for the case of the first letter. This is not good, it is confusing and bound to cause maintenance problems. OK, I'll also mention that using goto instead of a simple infinite loop was a programming faux pas.
// I'd like to thank everyone for exactly what I was looking for :D, where the main ingredient is multiple perspectives. I wanted to keep my question vague so that anyone could just look at it and go "this is what I would/wouldn't do, like/don't like.." etc. Most importantly, someone would say "try this" and another person would either agree or disagree and add more info.

// Basically, the whole thing was very educational because of multiple people! This is why I came to a forum, not to get one answer(because it seems like there's always a counter-point to everything), but to get multiple answers and see their multiple reasons for doing so. Obviously determining what's "right and wrong" for things isn't certain, and other cases it's beyond just simply "right or wrong".

// In other words yet again, I love you all. I learn so much from your discussions amongst each-other, whether you planned on it or not ^.^.
I think you would get even better comments if you post a non-trivial (but still short) program. Your original
program was pretty trivial in the sense that I could replace all the logic with a simple cout of the text that
the logic was hard-wired to display (as magik994's code shows).
Topic archived. No new replies allowed.