do while problem

I am working on a program , the do while part is not functioning .Can anyone please tell me what is wrong with the following code.
the error it shows is
syntax error : 'return'


do
{


cout << "Enter M to caluculate Miles Per gal , D for distance traveled , G for Gals used and Q to quit the program ";
cin >> ch ;

cout << ch << endl ;

switch (ch)
{
case 'm':
case 'M':
MilesperGal();
break ;
case 'd':
case 'D':
mileage();
break ;
case 'g':
case 'G':
fuelUsed ();
break;
default:
return 0;
} while (ch!='q' || ch!='Q');
}
return 0;


}
In the future:
- Please use [code][/code] tags
- Please tell what line the error occurs on

Your problem is because the 'while' is at the end of the switch{} and not at the end of the do{}. Therefore it errors saying "return is unexpected" because it expects a while statement after the do{} closes, and there isn't one.

Move your while() over.
fuelUsed ();
break;
default:
return 0;
}
while (ch!='q' || ch!='Q');

}
}


Sir ,This doesn't work either .further more can you please tell me how to use tags
.
Last edited on
That's because you didn't change anything, you just added a new line.

For do/while to work, the while has to be paired with the do. Right now it's paired with the switch.

1
2
3
4
5
6
7
8
// what you have (wrong)
do
{
  switch(...)
  {
    // stuff here
  } while(...);  // while at end of switch = no
}


what you need:

1
2
3
4
5
6
do
{
  switch(...)
  {
  }  // end of switch
} while(...);  // put while at end of do = good 


These kinds of errors are much easier to see with consistent indenting indenting.

As for code tags:

[code]
Paste your code between these tags
The # symbol on the right does the same thing
[/code]
Thank you for telling me about the code tags !
Is this what you mean ?

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

	switch (ch)
	{
	case 'm':
	case 'M':
		MilesperGal();
		break ;
	case 'd':
	case 'D':
		mileage();
		break ;
	case 'g':
	case 'G':
		fuelUsed ();
		break;
	default:
		return 0; 
	}
} while (ch!='q');
	return 0;
}
yes, exactly
It is still not working !
This is what it says


------ Rebuild All started: Project: afd, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'afd', configuration 'Debug|Win32'
Compiling...
fgfdgs.cpp
Compiling manifest to resources...
Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
Copyright (C) Microsoft Corporation. All rights reserved.
Linking...
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Users\Pro Standard F.S Co\Desktop\New Folder\afd\Debug\afd.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Users\Pro Standard F.S Co\Desktop\New Folder\afd\afd\Debug\BuildLog.htm"
afd - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Last edited on
99.9% of the time, "Unresolved External Symbol" means that you're calling a function that doesn't have a body.

Here, it's complaining about "_main", which is the entry point for the program. In order to have a C++ program, you need to have a function called "main" -- this is where your program starts.

1
2
3
4
5
6
7
8
int main()
{
  // your program executing code here

  // put program code here

  return 0;
}


Note that "program code" does not include stuff like #include statements or other functions. main() should've been one of the first things talked about in whatever class/book/tutorial you're using =x
I didn't get what you said .Here is my complete program
can you tell me where the problem is



[codevoid mileage()
{
double FuelUsed, MilesPerGal, Mileage;
cout << "Enter the fuel used = ";
cin >> FuelUsed;
cout << "Enter MPG = ";
cin >> MilesPerGal;

Mileage = FuelUsed * MilesPerGal;

cout << "Mileage = " << Mileage << endl;
}

void fuelUsed ()
{
double MilesPerGal, Mileage, FuelUsed;
cout << "Enter the Mileage used = ";
cin >> Mileage;
cout << "Enter mileage = ";
cin >> MilesPerGal;

FuelUsed = Mileage/MilesPerGal;

cout << "Fuel Used = " << FuelUsed << endl;

}
[/code]
Last edited on
That program compiles just fine for me. There's nothing [syntactically] wrong with it.

It might be a compiler configuration that's messed up. I'm afraid I will be of no use to you for solving this one. Sorry.
Topic archived. No new replies allowed.