Do/While loops

Hi everyone I'm new to a programming course and I don't find my lecturer 100% helpful, so I'm hoping maybe you people could give me a hand?

I'm not sure what to do in my do/while loop for stopping a sentinel value being processed; or why my "if else" is showing as an error.

I can post my code once I find out how unless it just a copy and paste as a response or something.

Eager to hear back as I want to do this before my night class
Thanks for any help :)
Yes, it is just copy paste. Copy your code in your compiler, then paste it here inside of the code format. You can use the button to the right or bottom, it looks like this "<>" without the quotes.

You can also just type it yourself. "code' "/code', (replace " with [ and ' with ] ) then paste your code inside of that. Sorry I don't know how to write out the source code parameters without it creating a code block.
Last edited on
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
#include<iostream>
using namespace std;
int main()
 // Declaring variable
 {float CountP=0, CountF=0, CountG=0, cons;
        int i;
        for(i=1; i<=10; i++)
  do
   {
   cout << "\n Enter in the fuel consumption of car: ";
   cin >> i;
   }
          // placing the consumption into good, poor and fair
          while(cons <7);
          {
             CountG= CountG + cons;
          }
          if(cons >=7 && cons <=10);
          {
             CountF= CountF + cons;
          }
          else if(cons>10);
          {
             CountP = CountP + cons;
          }
          
   cout << "\n The number of cars with Good fuel consumption is: " << CountG << endl;
   cout << "\n The number of cars with Fair fuel consumption is: " << CountF << endl;
   cout << "\n The number of cars with Poor fuel consumption is: " << CountP << endl;
   

system("pause");
return 0;
}
What error are you getting? I am very new to coding myself, but I can see what I think is a problem already. Your "if" statement doesn't need a ";" at the end of it. Like this.

1
2
3
if(cons >=7 && cons <=10)  // no semicolon needed here 
          {
             CountF= CountF + cons;


The same goes for the else statements.

I'm also not sure what you are asking for specifically when it comes to the do/while. Like I said I am pretty new myself, so someone more experienced may be able to help you.
Last edited on
@Ralden
To write the code tags so others can see it, you type[[b][/b]code][/code]

@BrendanKB
A do while is similar to a basic while, assuming you know how to do that. The syntax is this:
1
2
3
do {
   code;
} while (expression);


When the code reaches a do while, it will always run the code, at least once, and if the expression is true, it will loop the code again, until the expression evaluates to false.

As for the if/else, it's hard to say without the exact code. The syntax is:
1
2
3
4
5
6
7
8
if (condition) {
   // condition was true
   code;
}
else {
   // condition was false
   code;
}


A common issue with beginner programmers is putting a semicolon directly after the if condition. I don't think that would cause an issue, unless you put a semi colon directly after the closing brace of the if block or have a semicolon before the block:
1
2
3
4
5
6
7
8
// Error can be generated from this
if (condition); {
   code;
// or this
};
else {
   code;
}


Edit: After seeing your code, I can see how you're misunderstanding the lecturer. The while should remain on the same line as the close brace for the do/while loop. This will prevent confusion. Also, do/whiles are the only conditional loops/statements that I know of that actually require a semicolon after it.
Last edited on
I've made the changes like you said and I receive an error on line 28
1
2
3
4
          if else (cons>10)                   //This line has the error
          {
             CountP = CountP + cons;
          }


The error shows up as " Expected `(' before "else" "

I'm really stumped at this point.
Ahh, thank you Volatile Pulse. :)
Last edited on
Thanks Volatile Pulse and Ralden! I've made changes and it compiles and runs, except its not following my "for (initialization; condition; increase)"
it just keeps running after more than 10 loops so I must have it in the wrong order?
You need something for the if to do. It's saying that because it's expecting something before the else after the if.


Here is something very simple that may make it easier.


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
#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{
	int choice;

	cout << "Hello, please type 1 or 2:\n";

	cin >> choice;

	if (choice == 1)
		cout << "You typed 1!\n";
	else
		cout << "You typed 2!\n";


	char f;
	cin >> f;
	return 0;


}



So what is happening here is, the program asks you to type 1 or 2. Once you have type in either 1 or 2 it displays which one you typed.

IF (what you typed in is equal to the integer 1)
then we do this;
else
then we do this (if it is NOT what we asked in the if syntax)
Last edited on
You're missing you braces for the for loop. You should add them in to clarify to the compiler what you want it to run ten times. Another issue you're having is in your do/while, you're allowing the user to manually change the value of i. This can really mess up that for loop. And it does in your code.

Here is your code (slightly better formatted):
1
2
3
4
5
6
7
int i;
for (i = 1; i < 10; i ++) {
   do {
      cout << "\n Enter in the fuel consumption of car: ";
      cin >> i;
   } while (cons < 7);
}


You have unknowingly put your code in an infinite loop. The first time the for loop runs, i equals 1. You then ask the user to change the value of 1. Since you didn't assign the variable cons a value, more than likely it's going to be larger than 7 (some compilers force uninitialized variables to 0). This will make you while loop run forever. Since you can't complete the while loop, you also can't complete the for loop, not even one cycle. This is bad.

The idea of using a do/while is to prevent the need of other loops, such as the for loop. You are also assigning the wrong variable in your cin statement.
Well, you have to change a few things in your program...

EDIT: The code I just deleted had an ambiguous amount of errors, some of which were corrected and some of which were not. So I just deleted it.

1.) According to this, there is no need to declare CountP, CountF, and CountG as type float unless cons is (which you never really implemented in your program when you wrote it).
2.) You can't really have a for and do-while loop right after each other like volatile pulse said (at least not in this case). If you want the parameters of your for loop in your do-while loop, do this:
1
2
3
4
5
6
7
8
    int i=1;
    do
    {
        cout << "\n Enter in the fuel consumption of car: ";
        cin >> cons;
        i++;
    }
          while (i<=10);

3.) Everything in the squiggly brackets following "while" {} should actually be following if. What is currently in "if" should be under "else if", and what is under "else if" should be in another "else if" or an "else" (if you choose "else," don't put parameters such as (cons>10)). So this is the result:
1
2
3
4
5
6
7
8
9
10
11
12
13
 // placing the consumption into good, poor and fair
          if (cons <7)
          {
             CountG= CountG + cons;
          }
          else if(cons >=7 && cons <=10)
          {
             CountF= CountF + cons;
          }
          else if(cons>10)
          {
             CountP = CountP + cons;
          }

4.) After getting rid of the semicolons like the people above said already, and getting rid of system("pause") it should look like this:
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
#include<iostream>
using namespace std;
int main()
{
    // Declaring variable
    float CountP=0, CountF=0, CountG=0, cons;
    int i=1;
    do
    {
        cout << "\n Enter in the fuel consumption of car: ";
        cin >> cons;
        i++;
    }
          while (i<=10);

          // placing the consumption into good, poor and fair
          if (cons <7)
          {
             CountG= CountG + cons;
          }
          else if(cons >=7 && cons <=10)
          {
             CountF= CountF + cons;
          }
          else if(cons>10)
          {
             CountP = CountP + cons;
          }

   cout << "\n The number of cars with Good fuel consumption is: " << CountG << endl;
   cout << "\n The number of cars with Fair fuel consumption is: " << CountF << endl;
   cout << "\n The number of cars with Poor fuel consumption is: " << CountP << endl;

return 0;
}
Last edited on
Cheers :) I'll do the rest of it myself now so i can try problem solving :)
Both of you were great help!
The error in the first post is indeed the semi-colon after the if statement. The semi-colon itself is not a problem ("if this is true, execute nothing"). However, this makes "else if" (not "if else") something the compiler does not expect.

@Volatile, didn't you just make the same mistake there? Maybe I'm missing something:
1
2
3
4
5
6
7
8
 
int i;
for (i = 1; i < 10; i ++) {
   do {
      cout << "\n Enter in the fuel consumption of car: ";
      cin >> cons;
   } while (cons < 7);
}


But the whole thing doesnt really make sense to me anyway, why do-while at all?
1
2
3
4
5
6
7
8
9
for (int i = 0 ; i < 10 ; i++)
{
  cin >> cons;
  if (cons < 7)
    cout << "good\n";
  else if (...
}

// output data 


Lecturer said try it out and gave the class 3 questions to code up for tonight's class.
@LowestOne
How did I make a mistake? I was just showing the OP what the code he wrote really meant. I've noticed that a lot of beginners don't know "how" a program flows and sometimes it much more helpful to see it with proper indentation, proper control alignment, and proper scope resolution.
@LowestOne: That looks like a simpler way to do it...I don't know about you but the method I put up there gave some nonsense (the number of cars with good, fair, and poor fuel consumption was all 0).

EDIT @Volatile Pulse:
I've noticed that a lot of beginners don't know "how" a program flows

HA! You can say that again! (this isn't referring to you BrendanKB)
Last edited on
I was just referring to this:
1
2
for (int i = 0; i < 10 ; i++)
  cin >> i;

Thinking about it, maybe the intent is for "i" to be a sentinel, but even then it doesn't really work properly due to the increment.

The general use of do-while I use is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char choice;
cout << "Welcome to my program\n";
do
{
  cout << "Please make a choice\n"
       << "1) play\n"
       << "2) quit\n";
  cin >> choice;
  cin.ignore(80, '\n');

  if (choice == '1')
    cout << "Yee-Haa!\n";

}
while (choice != '2');
cout << "Thanks for playing\n";
Last edited on
I've been doing the next question coding and design and come so much closer than I have with this one at least I've learnt something :)

The 'i" is meant to be a repeat 10 times over then print out the CountG/F/P

Thanks for help new people :)

I'll probs come here a lot more cause I'm just not doing well in class and I'll try give back to the forums as much as I can :)
Topic archived. No new replies allowed.