Opportunity to ask a bunch of questions

Hello everybody,

At first i'm creating this thread to ask you guys something relating content from the basic c++ tutorial on this site, i already had a bit of skill relating programming with javascript etc... it always kinda cought my interest so i'm not a total dumbass.

i was at this part of the c++ tutorial that kinda confused me...
http://www.cplusplus.com/doc/tutorial/control.html

If you scroll down you will get to this part:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// number echoer

#include <iostream>
using namespace std;

int main ()
{
  unsigned long n;
  do {
    cout << "Enter number (0 to end): ";
    cin >> n;
    cout << "You entered: " << n << "\n";
  } while (n != 0);
  return 0;
}


Note that i fully understand the syntax and all of the while loop, but i do not understand the practical use of the "do" function.

I've written my own part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main(){
 
long number; 

while (number != 0)
{
      cout << "Give me a number please: ";
      cin >> number;
      cout << "The number you had given was: " << number << endl;
}

cout << " this line shouldn't be executed " << endl;

// required parameters...

return(0);
}


First of all they work both, i made the "long" variable: "number", but i didnt gave it any integer, i just defined the variable wich gives it the value of "NULL" insteed of the numerical "0"

Note that these are different things, NULL wich equals nothing and "0" wich is a numerical value...

Now you might wonder what my problem is... well there aint really a problem except for the fact that the one that wrote this tutorial didnt show any practical use for the "do" while,

Secondly he declared a variable:

unsigned long;

Wtf, unsigned long ???... you mean undefined long ?. I cant remember any topic wich explained the use of unsigned, only by coincidence i came to the conclusion that "long" contains more bytes then "int's" or "double's".

And finally, i didnt get where the "for" loop was good for... The only thing i can see in the for loop is harder syntax and variable's that are defined inside the bracets: "()"

The creator of the tutorial could have prevent a lot of confusion by typing that its only different, a mather of synthax and not of performance.

Now please don't get me wrong, i could have missed some points or overviewed some and thats wy i want you to reply on this thread.


Thank you
Jacob.
A do while loop will always loop once and then the condition is on the repeat. A while loop can run zero times, depending on the condition at the start.

I'll explain signed and unsigned using a char the same principle applies just the numbers are easier

a signed variable reserves the MSB as the sign, either negative or positive in value. An unsigned variable can only have a positive value. However there is a trade of in the sizes of the maximum number it can hold
1
2
3
signed char // -128 to +127
unsigned char // 0 to +255
char // can be either of the above although by convention, it is usually regarded as signed 


All signed variable types reserve the MSB for the sign. The char sign is implicitly decided by the compiler at compile time, depending on what values are stored in it. Do not mix an implicit variables' use between signed and unsigned values otherwise you'll get some very strange numbers
Thanks for your reply, i get the signed unsigned part now... char has a size of 255 but standard half of it is a negative value.

About the do part, i know what you mean but the one that wrote the example didnt show any practical use of it, since my example without a do works aswell.
I can't think of a good example off the top of my head, maybe someone else can. There are lots of cases where both methods will do the job, then it is down to personal preference.
Last edited on
I think indeed the function hasn't been made if it would not have any purpose at all, i know the syntax of it... once i indeed hit a problem where i have to use this it will clear a lot of confusion ^^.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
     int number1, number 2;
     char repeat;
     do{
          cout << "Enter two numbers to add" << endl;
          cin >> number1 >> number2;
          cout << number1 + number2 << endl;
          cout << "Again? y/n" << endl;
          cin >> repeat;
     }while(repeat == 'y');
     return 0;
}

I don't know if that makes since but I use it when I know I want to run at least once like bnbertha said.

Also the for loop has many uses. Like initializing arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
      int Cards[52];
      for(int i = 0; i < 52; i++)
              Cards[i] = i % 13 + 1;
      for(int j = 0; j < 52; j++)
              cout << Cards[j] << endl;
      system("pause");
      return 0;
}


I dunno, I use it for a lot of stuff;
Last edited on
mmh... well in any case loops always work with the goto function aswell, your first example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
numbers:
int number1, number2;
char repeat;
cout << "Please give me 2 numbers: ";
cin >> number1 >> number2;
cout << number1 + number2 << "\nAgain ? y/n";
cin >> repeat;
if(repeat == 'y') goto numbers;
cout << "\nProgram terminated ^_^ ?\n\n";
system("pause");
return(0);
}


I didnt understand your second example, the compiler did not compile it, i try'd modifying it but still didn't work, also i'm not at the array level yet but if you could modify the second example so that it would work, that would be nice.

As for the first example wich where you forgot to write "std;".
Last edited on
Sorry I wrote those on here and didn't even try to compile them, I'll fix em.

They'll compile now. Arrays are easy, just think of them for now as a list of variables. Cards[0] is an int, Cards[1] is an int and so on.

And I would shy away from goto, someone else could probably explain why not to use them better than me.
Last edited on
Hello again mikeb570,

I was taking a look at your card sorter and i didnt really understood the script, so i decided to rewrite it using a while... but i got confused and stopped writing, its not counting from "X to 0" insteed it just gives some clean number.

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
#include <iostream>
using namespace std;

int main(){
    
    cout << "Cards will now shuffle i guese...\n\n";
    int Cards = 52, Players = 5, devided, Players2;
    devided = Cards/Players;
    Players2 = Players;
    
    while (devided > 0 & Players2 > 0){
          
          cout << devided << endl;
          devided--;
          
          if ( devided != 1 ){
          devided = Cards/Players;    
          Players2--;
                     }
          else;
          }
          
    system("pause");
    return 0;
}


If you could explain me how your example works and comment on my script.
I made one script before but i lost it, that one counted from the devided cards back to "0", but then as in your example it needs to redo the same action severall times ( Being the players ).
Last edited on
Well my example isn't a card sorter, it is just making a deck(array) of 52 ints and giving values like cards. Learn for loops and maybe that will give you a better understand of how loops work and what they are good for.

I'm confused about the logic in your example but I'll tell you some of your errors.

'&' is Bitwise AND, which will not yield the results you would be expecting. Use '&&'.

line 8
 
devided = Cards/Players;


Devided is an int, meaning it will always truncate (round down). so 52/5 is 10.4 but the value of devided will be 10.

Your while loop needs an opening brace.

For loops are easy.

1
2
3
4
5
6
7
8
int main()
{ 
      for(int i = 10; i > 0; i--)
      {
             cout << i << endl;
      }    
      cout << "Blast off!" << endl;
}


So we have 3 statements in the parenthesis of a for loop

for(1 ; 2 ; 3)

1)You can make and initialize an int like I did, I usually use i. That's all.

2)Repeat while this statement is true. e.g. i < 10.

3)What to do every iteration through the loop. Everytime my example goes through it subtracts one from i.

They are good for when you know you want to do something a set number of times and plenty of other things.
Last edited on
Topic archived. No new replies allowed.