using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_basic2
{
class Program
{
static void Main(string[] args)
{
int a, b, c, avg, total, number, counter, counter2;
double fact=1;
string exit, option;
Console.WriteLine("Press 1 To Find Your Grade--> ");
Console.WriteLine("Press 2 To Find Factorial Of Number--> ");
Console.WriteLine("Press 3 To Display Odd Numbers--> ");
Console.WriteLine("Press 4 To Exit The Program --> ");
option = Console.ReadLine();
switch (option) // switch control structure
{
case "1":
Console.WriteLine("enter your marks in Physics");
a = Int32.Parse(Console.ReadLine());
Console.WriteLine("enter your marks in Chemistry");
b = Int32.Parse(Console.ReadLine());
Console.WriteLine("enter your marks in Maths");
c = Int32.Parse(Console.ReadLine());
total = a + b + c;
avg = total / 3;
Console.WriteLine("your total marks are {0} out of 300", total);
if (avg >= 90) //if else control structure
Console.WriteLine("You got A grade");
else
if (avg >= 80)
Console.WriteLine("You got B grade ");
else
if (avg >= 60)
Console.WriteLine("you got C grade");
else
Console.WriteLine("Bad News Buddy ..! You are Failed");
break;
case "2":
Console.WriteLine("enter the number to find its factorial");
number = Int32.Parse(Console.ReadLine());
for (counter = 1; counter <= number; counter++) // for repetitive structure
{
fact = fact * counter;
}
Console.WriteLine("factorial is {0} ", fact);
break;
case "3":
Console.WriteLine("press any number upto which u wanna find odd numbers ");
counter2 = Int32.Parse(Console.ReadLine());
int n = 1;
while (n < counter2) // while repetitive structure
{
if (n % 2 == 0)
{
n++;
}
else
{
Console.WriteLine(" " + n);
n++;
}
}
break;
case "4":
Console.WriteLine("press 4 to exit the program ");
do // do/while repetitive structure
{
exit = Console.ReadLine();
if (exit != "4") // if control structure
Console.WriteLine("you havn't entered right key " + ". " + "press 4 to exit the program ");
Console.WriteLine("press 4 to exit the program ");
}
while (exit != "4");
break;
default:
Console.WriteLine("You Didn't Enter The Right Option");
break;
}
}
}
}
this is source of my program..all program is running well...
i
mean no syntex error,compilation error,or run time error...and also all 4 option too but in 4 option..
.i
have applied do-if-while loop... according to program if i press 4 then it will exit the prog....
but
when i press 4 it first print statement after the if loop......then exit... why this is happening i don't know.... ??
is
this some kind of bug..if we use if in do/while loop.... i have tried message box too instead of message printing..but same result.... does anyone know the answer..!!!!
2. The first statement in case "4", tells the user "press 4 to exit the program"
3. The do loop is entered.
4. Inside the do loop, the user is required to enter test at the console.
5. The results of that entry are checked against "4". If it is not "4", the "you havn't entered..." message is displayed.
6. After the if statement, the message "press 4 to exit the program is displayed.
It isn't clear what behavior you want to me.
Maybe you want something like this for your if statement:
if(exit != "4") {
Console.WriteLine("you havn't entered right key " + ". " + "press 4 to exit the program ");
Console.WriteLine("press 4 to exit the program ");
}
The curley braces mean that both of those commands are only executed if the if conditional is true.
case "4":
Console.WriteLine("press 4 to exit the program ");
do // do/while repetitive structure
{
exit = Console.ReadLine();
if (exit != "4") // if control structure
Console.WriteLine("you havn't entered right key " + ". " + "press 4 to exit the program ");
Console.WriteLine("press 4 to exit the program ");
}
while (exit != "4");
break;
default:
Console.WriteLine("You Didn't Enter The Right Option");
break;
now i tell u more clearly...
when u press 4 it goes to 2nd line and displayes message..
then u again enter 4 ......
now here is big twist....
according to program it exits.....but this is not exactly happens..
when u enter 4 and hit enter it first goes to line 8 and displayes message and exit the program...
now what is this is this some kind of bug..
i also tried with message box...but same results..
This is the correct behaviour for the do while cicle.
If the problem is the display of a "press 4 to exit the program" that's because that line should not be there. You've already have this information in line 7 and line 2 when pressed for the first time.
case "4":
do // do/while repetitive structure
{
Console.WriteLine("press 4 to exit the program ");
exit = Console.ReadLine();
if (exit != "4") // if control structure
Console.WriteLine("you havn't entered right key.");
} while (exit != "4");
break;
i think any of u didn't run this program.. guys copy this whole program and paste in then run.. i am saying tht all program is working fine ..there is no type of error.. please run ths program and see the output.. when u run this program.. 1. it will display
statement "press 4 to exit" 2. when u press 4 ..then it print the statement..after the if loop then exits the program.. now why it is going back in if loop when pressing 4...it should exit the program...
Oh, really? Did you copy my code and paste it in to your program? When I did that it worked just the way you described your requirement. IF you don't think it works the way you want it to then I suggest you try this
Run the program, with my code in place of your case "4". Write down the output exactly as it appears in your console window (line-for-line and character-for-character, no abbreviations, no shorthand, no SMS speak) Then write down exactly what you would
like to see from the very start of running the program to the very end of running the program (this will become your specification). Post both sets of output (the actual and the desired) and someone in this forum will show you how to achieve your goal. Rest
assured you have not found a bug in c#.
People in this forum give their time freely as a community service. It is not up to us to go to great length to debug your code. Did you even use the debugger to step through your code? This forum helps those who help themselves.
mahajan344
Member
124 Points
99 Posts
[c#] is this some kind of bug..
Feb 24, 2009 09:13 AM|LINK
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace C_basic2 { class Program { static void Main(string[] args) { int a, b, c, avg, total, number, counter, counter2; double fact=1; string exit, option; Console.WriteLine("Press 1 To Find Your Grade--> "); Console.WriteLine("Press 2 To Find Factorial Of Number--> "); Console.WriteLine("Press 3 To Display Odd Numbers--> "); Console.WriteLine("Press 4 To Exit The Program --> "); option = Console.ReadLine(); switch (option) // switch control structure { case "1": Console.WriteLine("enter your marks in Physics"); a = Int32.Parse(Console.ReadLine()); Console.WriteLine("enter your marks in Chemistry"); b = Int32.Parse(Console.ReadLine()); Console.WriteLine("enter your marks in Maths"); c = Int32.Parse(Console.ReadLine()); total = a + b + c; avg = total / 3; Console.WriteLine("your total marks are {0} out of 300", total); if (avg >= 90) //if else control structure Console.WriteLine("You got A grade"); else if (avg >= 80) Console.WriteLine("You got B grade "); else if (avg >= 60) Console.WriteLine("you got C grade"); else Console.WriteLine("Bad News Buddy ..! You are Failed"); break; case "2": Console.WriteLine("enter the number to find its factorial"); number = Int32.Parse(Console.ReadLine()); for (counter = 1; counter <= number; counter++) // for repetitive structure { fact = fact * counter; } Console.WriteLine("factorial is {0} ", fact); break; case "3": Console.WriteLine("press any number upto which u wanna find odd numbers "); counter2 = Int32.Parse(Console.ReadLine()); int n = 1; while (n < counter2) // while repetitive structure { if (n % 2 == 0) { n++; } else { Console.WriteLine(" " + n); n++; } } break; case "4": Console.WriteLine("press 4 to exit the program "); do // do/while repetitive structure { exit = Console.ReadLine(); if (exit != "4") // if control structure Console.WriteLine("you havn't entered right key " + ". " + "press 4 to exit the program "); Console.WriteLine("press 4 to exit the program "); } while (exit != "4"); break; default: Console.WriteLine("You Didn't Enter The Right Option"); break; } } } }this is source of my program..all program is running well...
i mean no syntex error,compilation error,or run time error...and also all 4 option too but in 4 option..
.i have applied do-if-while loop... according to program if i press 4 then it will exit the prog....
but when i press 4 it first print statement after the if loop......then exit... why this is happening i don't know.... ??
is this some kind of bug..if we use if in do/while loop.... i have tried message box too instead of message printing..but same result.... does anyone know the answer..!!!!
C#
Alan Ourslan...
Member
187 Points
29 Posts
Re: [c#] is this some kind of bug..
Feb 24, 2009 06:42 PM|LINK
Walk through your case 4.
1. The users types "4" at the top menu.
2. The first statement in case "4", tells the user "press 4 to exit the program"
3. The do loop is entered.
4. Inside the do loop, the user is required to enter test at the console.
5. The results of that entry are checked against "4". If it is not "4", the "you havn't entered..." message is displayed.
6. After the if statement, the message "press 4 to exit the program is displayed.
It isn't clear what behavior you want to me.
Maybe you want something like this for your if statement:
if(exit != "4") { Console.WriteLine("you havn't entered right key " + ". " + "press 4 to exit the program "); Console.WriteLine("press 4 to exit the program "); }The curley braces mean that both of those commands are only executed if the if conditional is true.
Good luck in your class.
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: [c#] is this some kind of bug..
Feb 24, 2009 06:42 PM|LINK
The program is doing exactly what you are telling it to do.
My blog
mahajan344
Member
124 Points
99 Posts
Re: [c#] is this some kind of bug..
Feb 25, 2009 12:12 PM|LINK
no you are not getting my point..program is running fine..but there is prob with 4 option..
according to 4th option
1. when i press 4 it will enter in option 4.
2. messages will display thh" press 4 to exit the program".
3. now when i press 4
then check guys its result..
first it displays the message..ie.. statement in if loop..."press 4 to exit the program".. then it exit..
now why it is displaying tht message ...rather to simple exit..
tht my question...
just run the 4 option guys u will get it wht i am saying..and see clearly the output,,,
mahajan344
Member
124 Points
99 Posts
Re: [c#] is this some kind of bug..
Feb 25, 2009 12:17 PM|LINK
case "4": Console.WriteLine("press 4 to exit the program "); do // do/while repetitive structure { exit = Console.ReadLine(); if (exit != "4") // if control structure Console.WriteLine("you havn't entered right key " + ". " + "press 4 to exit the program "); Console.WriteLine("press 4 to exit the program "); } while (exit != "4"); break; default: Console.WriteLine("You Didn't Enter The Right Option"); break;now i tell u more clearly...
when u press 4 it goes to 2nd line and displayes message..
then u again enter 4 ......
now here is big twist....
according to program it exits.....but this is not exactly happens..
when u enter 4 and hit enter it first goes to line 8 and displayes message and exit the program...
now what is this is this some kind of bug..
i also tried with message box...but same results..
rumbafum
Participant
1162 Points
247 Posts
Re: [c#] is this some kind of bug..
Feb 25, 2009 04:38 PM|LINK
This is the correct behaviour for the do while cicle.
If the problem is the display of a "press 4 to exit the program" that's because that line should not be there. You've already have this information in line 7 and line 2 when pressed for the first time.
Remove line 8 instruction
Alan Ourslan...
Member
187 Points
29 Posts
Re: [c#] is this some kind of bug..
Feb 25, 2009 09:05 PM|LINK
I think he wants it to say "press 4 to exit the program" if the user didn't press 4 a second time. See my original answer to fix that.
Although it doesn't make much sense to me to keep looping on that statement requiring the user to enter '4' to exit with no way to exit.
Paul Linton
Star
13403 Points
2531 Posts
Re: [c#] is this some kind of bug..
Feb 25, 2009 10:26 PM|LINK
Rearrange your case '4' like this
case "4": do // do/while repetitive structure { Console.WriteLine("press 4 to exit the program "); exit = Console.ReadLine(); if (exit != "4") // if control structure Console.WriteLine("you havn't entered right key."); } while (exit != "4"); break;mahajan344
Member
124 Points
99 Posts
Re: [c#] is this some kind of bug..
Feb 26, 2009 04:15 AM|LINK
Paul Linton
Star
13403 Points
2531 Posts
Re: [c#] is this some kind of bug..
Feb 26, 2009 04:44 AM|LINK
Oh, really? Did you copy my code and paste it in to your program? When I did that it worked just the way you described your requirement. IF you don't think it works the way you want it to then I suggest you try this
Run the program, with my code in place of your case "4". Write down the output exactly as it appears in your console window (line-for-line and character-for-character, no abbreviations, no shorthand, no SMS speak) Then write down exactly what you would like to see from the very start of running the program to the very end of running the program (this will become your specification). Post both sets of output (the actual and the desired) and someone in this forum will show you how to achieve your goal. Rest assured you have not found a bug in c#.
People in this forum give their time freely as a community service. It is not up to us to go to great length to debug your code. Did you even use the debugger to step through your code? This forum helps those who help themselves.