Can anyone tell me what "Break" is used here in this C++ sentence? Anyone who answer 1st will get 10 points!
#include %26lt;stdio.h%26gt;
#include %26lt;conio.h%26gt;
int main(void)
{
for(; ;)
{
if (getche() == 'a')
{
auto int t;
for (t=0; t%26lt;'a'; t++)
printf("%d ", t);
break;
}
}
return 0;
}
"Break" in this C++ programming?
Break is used within loops and switch statements to jump to the end of the code block. It causes the "//code..." above to be skipped and terminates the loop. In switch case statements, break causes the remaining cases to be skipped--it prevents "falling through" to other cases.
Reply:It terminates the outer for loop " for(;;) ", which has no explicit termination condition.
Reply:here you want to print t - a no of times;
break is used to come out of if loop once the second for loop
has run its course.
if you dont use break , then for the first or loop
which is having no arguments or parameters
-it can end in infinite loop.
sp
Monday, May 24, 2010
C programme...?
I wrote a programme of calculator in C language. And it has two or three error so pl do correct them. And send me correct answer.
Prog. is:
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
void calsum(int a,int b);
void calsub(int a,int b);
void calmul(int a,int b);
void caldiv(float a,float b);
/*void calmod(int a,int b); */
void calsqr(int a);
void calcue(int a);
void calfac(long int a);
void main()
{
int l,m,n,ch;
clrscr();
printf("\t\t==========\n");
printf("\t\t Menu ");
printf("\t\t==========\n");
printf("\t\t 1 Add\n");
printf("\t\t 2 Sub\n");
printf("\t\t 3 Mul\n");
printf("\t\t 4 Div\n");
printf("\t\t 5 Factorail\n");
printf("\t\t 6 Square\n");
printf("\t\t 7 Cube\n");
/*printf("\t\t 8 Mod\n"); */
printf("\t\t Enter your choice \n");
scanf("%d",ch);
printf("\t\t =====\t");
switch(ch)
{
case 1:
printf("\t\tEnter two number : ");
scanf("%d%d",%26amp;l,%26amp;m);
calsum(l,m);
break;
case 2:
printf("\t\tEnter two numbers " );
scanf("%d%d",%26amp;l,%26amp;m);
calsub(l,m);
break;
case 3:
printf("\t\t Enter two numbers" );
scanf("%d%d",%26amp;l,%26amp;m);
calmul(l,m);
break;
case 4:
printf("\t\t Enter two numbers : ");
scanf("%s%s",%26amp;l,%26amp;m);
caldiv(l,m);
break;
case 5:
printf("\t\t Enter any no : ");
scanf("%ld",%26amp;l);
calfac(l);
break;
case 6:
printf("\t\t Enter any no : ");
scanf("%d",%26amp;l);
calsqr(l);
break;
case 7:
printf("\t\t Enter any no : ");
scanf("%d",%26amp;l);
calcue(l);
break;
/*
case 8:
printf("\t\t Enter any no : ");
scanf("%d",%26amp;l);
calmod(l);
break;*/
default:
printf("\t It is wrong choice : ");
}
getch();
}
void calsum(int a,int b)
{
int c;
c=a+b;
printf("Addition=%d",c);
}
void calsub(int a,int b)
{
int c;
c=a-b;
printf("Substance=%d",c);
}
void calmul(int a,int b)
{
int c;
c=a*b;
printf("Multiply=%d",c);
}
void caldiv(float a,float b)
{
float c;
c=a/b;
printf("Divide=%d",c);
}
void calsqr(int a)
{
int c;
c=a*a;
printf("Square=%d",c);
}
void calcue(int a)
{
int c;
c=a*a*a;
printf("Cube=%d",c);
} /*
void calmod(int a,int b)
{
int c;
c=a%b;
printf("Mod=%d",c);
} */
void calfac(long int a)
{
long int i,fct;
fct=1;
for(i=a;i%26gt;=1;i--)
{
fct=fct*i;
}
printf("Factorail=%d",fct);
}
C programme...?
/*Simple Calculator*/
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{
int a,b,C;
char ch;
clrscr();
printf("Enter any two numbers:\n");
scanf("%d%d",%26amp;a,%26amp;b);
printf("The operation to be performed is: \n");
fflush(stdin);
scanf("%c",%26amp;ch);
switch(ch)
{
case '+':
C=a+b;
break;
case '-':
C=a-b;
break;
case '*':
C=a*b;
break;
case '/':
C=a/b;
break;
default:
printf("character is invalid");
}
printf("Answer is %d",C);
getch();
}
/* Output */
Enter any two numbers:
24 2
Enter any character
/
Answer is 12
Prog. is:
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
void calsum(int a,int b);
void calsub(int a,int b);
void calmul(int a,int b);
void caldiv(float a,float b);
/*void calmod(int a,int b); */
void calsqr(int a);
void calcue(int a);
void calfac(long int a);
void main()
{
int l,m,n,ch;
clrscr();
printf("\t\t==========\n");
printf("\t\t Menu ");
printf("\t\t==========\n");
printf("\t\t 1 Add\n");
printf("\t\t 2 Sub\n");
printf("\t\t 3 Mul\n");
printf("\t\t 4 Div\n");
printf("\t\t 5 Factorail\n");
printf("\t\t 6 Square\n");
printf("\t\t 7 Cube\n");
/*printf("\t\t 8 Mod\n"); */
printf("\t\t Enter your choice \n");
scanf("%d",ch);
printf("\t\t =====\t");
switch(ch)
{
case 1:
printf("\t\tEnter two number : ");
scanf("%d%d",%26amp;l,%26amp;m);
calsum(l,m);
break;
case 2:
printf("\t\tEnter two numbers " );
scanf("%d%d",%26amp;l,%26amp;m);
calsub(l,m);
break;
case 3:
printf("\t\t Enter two numbers" );
scanf("%d%d",%26amp;l,%26amp;m);
calmul(l,m);
break;
case 4:
printf("\t\t Enter two numbers : ");
scanf("%s%s",%26amp;l,%26amp;m);
caldiv(l,m);
break;
case 5:
printf("\t\t Enter any no : ");
scanf("%ld",%26amp;l);
calfac(l);
break;
case 6:
printf("\t\t Enter any no : ");
scanf("%d",%26amp;l);
calsqr(l);
break;
case 7:
printf("\t\t Enter any no : ");
scanf("%d",%26amp;l);
calcue(l);
break;
/*
case 8:
printf("\t\t Enter any no : ");
scanf("%d",%26amp;l);
calmod(l);
break;*/
default:
printf("\t It is wrong choice : ");
}
getch();
}
void calsum(int a,int b)
{
int c;
c=a+b;
printf("Addition=%d",c);
}
void calsub(int a,int b)
{
int c;
c=a-b;
printf("Substance=%d",c);
}
void calmul(int a,int b)
{
int c;
c=a*b;
printf("Multiply=%d",c);
}
void caldiv(float a,float b)
{
float c;
c=a/b;
printf("Divide=%d",c);
}
void calsqr(int a)
{
int c;
c=a*a;
printf("Square=%d",c);
}
void calcue(int a)
{
int c;
c=a*a*a;
printf("Cube=%d",c);
} /*
void calmod(int a,int b)
{
int c;
c=a%b;
printf("Mod=%d",c);
} */
void calfac(long int a)
{
long int i,fct;
fct=1;
for(i=a;i%26gt;=1;i--)
{
fct=fct*i;
}
printf("Factorail=%d",fct);
}
C programme...?
/*Simple Calculator*/
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{
int a,b,C;
char ch;
clrscr();
printf("Enter any two numbers:\n");
scanf("%d%d",%26amp;a,%26amp;b);
printf("The operation to be performed is: \n");
fflush(stdin);
scanf("%c",%26amp;ch);
switch(ch)
{
case '+':
C=a+b;
break;
case '-':
C=a-b;
break;
case '*':
C=a*b;
break;
case '/':
C=a/b;
break;
default:
printf("character is invalid");
}
printf("Answer is %d",C);
getch();
}
/* Output */
Enter any two numbers:
24 2
Enter any character
/
Answer is 12
Any Idea What this C Code is all about??
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
#include%26lt;stdlib.h%26gt;
void main()
{
char *a = "main(){char *a= %c%s%c; printf(a,34,a,34); }";
printf(a,34,a,34);
getch();
}
Could someone explain the Logic behind this??
Any Idea What this C Code is all about??
Compiler converts the third line i.e. Printf(a,34,a,34); to this :-
printf("main(){char *a= %c%s%c; printf(a,34,a,34); }", 34, "main(){char *a= %c%s%c; printf(a,34,a,34); }", 34)
when the program is executed the first argument of the printf statement i.e 'main(){char *a= %c%s%c; printf(a,34,a,34); }' is executed in the following way :
main(){char *a= : gets printed as is
%c : this converts the second argument of the printf ie 34 to character and prints it. The output is '"' whose ascii value is 34.
%s : this outputs the third argument of the Printf Statement ie. 'main(){char *a= %c%s%c; printf(a,34,a,34); }'.
%c : this converts the fourth argument of the printf ie 34 to character and prints it. The output is '"' whose ascii value is 34.
;printf(a,34,a,34); } : gets printed as is.
so the output is
main(){char *a= "main(){char *a= %c%s%c; printf(a,34,a,34); }";printf(a,34,a,34); }
Reply:this is simply illogical. I'm not sure the pointer variable *a that has char type could hold he value of a string (which is an array of char in the inside). This program is illogical, where do you get this? And what it said it do?
Reply:Well I know C++, I think they are pretty much the same.. and from what I am seeing.. uh.. this is a bit confusing.. its seeming like you are creating a pointer that is going to be pointing a place in memory that will contain character data.. then its like going to display main(){char *a= %c%s%c; printf(a,34,a,34); }34main(){char *a= %c%s%c; printf(a,34,a,34); }34 to the console screen.. Its a bit odd.. hmm, Have you tested this code out? to me don't look like it would work.. but I could be wrong..
#include%26lt;conio.h%26gt;
#include%26lt;stdlib.h%26gt;
void main()
{
char *a = "main(){char *a= %c%s%c; printf(a,34,a,34); }";
printf(a,34,a,34);
getch();
}
Could someone explain the Logic behind this??
Any Idea What this C Code is all about??
Compiler converts the third line i.e. Printf(a,34,a,34); to this :-
printf("main(){char *a= %c%s%c; printf(a,34,a,34); }", 34, "main(){char *a= %c%s%c; printf(a,34,a,34); }", 34)
when the program is executed the first argument of the printf statement i.e 'main(){char *a= %c%s%c; printf(a,34,a,34); }' is executed in the following way :
main(){char *a= : gets printed as is
%c : this converts the second argument of the printf ie 34 to character and prints it. The output is '"' whose ascii value is 34.
%s : this outputs the third argument of the Printf Statement ie. 'main(){char *a= %c%s%c; printf(a,34,a,34); }'.
%c : this converts the fourth argument of the printf ie 34 to character and prints it. The output is '"' whose ascii value is 34.
;printf(a,34,a,34); } : gets printed as is.
so the output is
main(){char *a= "main(){char *a= %c%s%c; printf(a,34,a,34); }";printf(a,34,a,34); }
Reply:this is simply illogical. I'm not sure the pointer variable *a that has char type could hold he value of a string (which is an array of char in the inside). This program is illogical, where do you get this? And what it said it do?
Reply:Well I know C++, I think they are pretty much the same.. and from what I am seeing.. uh.. this is a bit confusing.. its seeming like you are creating a pointer that is going to be pointing a place in memory that will contain character data.. then its like going to display main(){char *a= %c%s%c; printf(a,34,a,34); }34main(){char *a= %c%s%c; printf(a,34,a,34); }34 to the console screen.. Its a bit odd.. hmm, Have you tested this code out? to me don't look like it would work.. but I could be wrong..
C++ EXPERTS!! HELP!! Please??
I use:
#include %26lt;iostream.h%26gt;
main()
{
cout %26lt;%26lt; "Hello World!";
return 0;
}
In MS Visual C++ and always get the same error!!
1%26gt;c:\documents and settings\c j smith\my documents\visual studio 2005\projects\test\test\test.cpp(6) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
1%26gt;Build log was saved at "file://c:\Documents and Settings\c j smith\My Documents\Visual Studio 2005\Projects\test\test\Debug\BuildLog.h...
1%26gt;test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And a pop up?
Unable to start program c:\documents and settings c j smith\my documents\visual studio 2005\projects\test\debug\test/exe.
The system cannot find the file specified.
PLEASE HELP!
C++ EXPERTS!! HELP!! Please??
It should be this:
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
cout %26lt;%26lt; "Hello World!";
return 0;
}
Reply:The ".h" in "iostream.h" is archaic and should not be used. That line should read:
#include %26lt;iostream%26gt;
Also, you might want to give main() a type.... int main() and void main() are both acceptable.
#include %26lt;iostream.h%26gt;
main()
{
cout %26lt;%26lt; "Hello World!";
return 0;
}
In MS Visual C++ and always get the same error!!
1%26gt;c:\documents and settings\c j smith\my documents\visual studio 2005\projects\test\test\test.cpp(6) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
1%26gt;Build log was saved at "file://c:\Documents and Settings\c j smith\My Documents\Visual Studio 2005\Projects\test\test\Debug\BuildLog.h...
1%26gt;test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And a pop up?
Unable to start program c:\documents and settings c j smith\my documents\visual studio 2005\projects\test\debug\test/exe.
The system cannot find the file specified.
PLEASE HELP!
C++ EXPERTS!! HELP!! Please??
It should be this:
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
cout %26lt;%26lt; "Hello World!";
return 0;
}
Reply:The ".h" in "iostream.h" is archaic and should not be used. That line should read:
#include %26lt;iostream%26gt;
Also, you might want to give main() a type.... int main() and void main() are both acceptable.
Beginner C++ Help?
Hi,I'm new to C++. And I mean literally new, as in I just started yesterday. I've read a few tutorials and learned somethings but I still know basically nothing. I'm making some simple programs to practice and the current one is a random number generator. Here is the code.
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int a,b,c,d,e,f;
string g,h="a";
loop:
a = (rand()%10);
b = (rand()%10);
c = (rand()%10);
d = (rand()%10);
e = (rand()%10);
f = (rand()%10);
cout%26lt;%26lt;"\n";
cout %26lt;%26lt; a;
cout %26lt;%26lt; b;
cout %26lt;%26lt; c;
cout %26lt;%26lt; d;
cout %26lt;%26lt; e;
cout %26lt;%26lt; f;
cout%26lt;%26lt;"\n";
cout%26lt;%26lt;"\n";
cout%26lt;%26lt;"To create a new random number, press a and then enter.";
getline (cin,g);
if (g==h){
goto loop;
}
cin.get();
}
Beginner C++ Help?
As Ahmad said, you need to seed the PRNG. srand with time as an argument works well. But I have a few things to say about your code.
The first is your variable names. Please choose more meaningful variable names. You realize they can be more than one letter long right?
Do not use goto and labels. They have their use, but no beginner and most intermediates will encounter such a situation. There are flow control constructs, like for loops, while loops, and do while loops. Use them. Your code could be rewritten to use a do-while loop and two for loops.
There’s something called an array. You should learn it as soon as possible.
Reply:You need to seed the random number generator.
http://www.cprogramming.com/tutorial/ran...
radiata
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int a,b,c,d,e,f;
string g,h="a";
loop:
a = (rand()%10);
b = (rand()%10);
c = (rand()%10);
d = (rand()%10);
e = (rand()%10);
f = (rand()%10);
cout%26lt;%26lt;"\n";
cout %26lt;%26lt; a;
cout %26lt;%26lt; b;
cout %26lt;%26lt; c;
cout %26lt;%26lt; d;
cout %26lt;%26lt; e;
cout %26lt;%26lt; f;
cout%26lt;%26lt;"\n";
cout%26lt;%26lt;"\n";
cout%26lt;%26lt;"To create a new random number, press a and then enter.";
getline (cin,g);
if (g==h){
goto loop;
}
cin.get();
}
Beginner C++ Help?
As Ahmad said, you need to seed the PRNG. srand with time as an argument works well. But I have a few things to say about your code.
The first is your variable names. Please choose more meaningful variable names. You realize they can be more than one letter long right?
Do not use goto and labels. They have their use, but no beginner and most intermediates will encounter such a situation. There are flow control constructs, like for loops, while loops, and do while loops. Use them. Your code could be rewritten to use a do-while loop and two for loops.
There’s something called an array. You should learn it as soon as possible.
Reply:You need to seed the random number generator.
http://www.cprogramming.com/tutorial/ran...
radiata
I have a doubt in c++ program?
i have an error in c++ program.......its coming as an error 4 all the programs......the error is "fatal..\INCLUDE\CONIO%26gt;H 165: error directive: must use c++ for the type iostream". so wat correction am i supposed to make?
I have a doubt in c++ program?
I think you wrote a program as C++, but saved the file as a C file. For example, instead of naming the file MyProg.cpp, you called it MyProg.c. The compiler uses the file extension to determine if the program is a C program or a C++ program.
Reply:it is the error of your header files......either your library is corrupter or you need to set ur path by going in DOS shell.........
Reply:I wish you had provided your code because this looks like a relatively simple problem but wihtout your code it can be many types of things but one thing is for certain, thre previous responder (above this comment) is wrong in saying the file could be corrupt (that is just silly).....
Could you paste your code so i can take a look? Also it might be possible that your dev environment is set up to think its C specific or C++ (depending on which type you are trying to compile in)..
you should have either (or both):
#include %26lt;conio%26gt;
#include %26lt;iostream%26gt;
Notice you dont need to mention the ".h" extension for the conio portion if you are working with C++ code....And notice i didn't give the path to the library...the system knows where theya re already.
I have a doubt in c++ program?
I think you wrote a program as C++, but saved the file as a C file. For example, instead of naming the file MyProg.cpp, you called it MyProg.c. The compiler uses the file extension to determine if the program is a C program or a C++ program.
Reply:it is the error of your header files......either your library is corrupter or you need to set ur path by going in DOS shell.........
Reply:I wish you had provided your code because this looks like a relatively simple problem but wihtout your code it can be many types of things but one thing is for certain, thre previous responder (above this comment) is wrong in saying the file could be corrupt (that is just silly).....
Could you paste your code so i can take a look? Also it might be possible that your dev environment is set up to think its C specific or C++ (depending on which type you are trying to compile in)..
you should have either (or both):
#include %26lt;conio%26gt;
#include %26lt;iostream%26gt;
Notice you dont need to mention the ".h" extension for the conio portion if you are working with C++ code....And notice i didn't give the path to the library...the system knows where theya re already.
Question on c I am using linux/unix to write my code?
This is an external function from a big program i am doing a stub test because if i run the whole program the questions overload and i cannot input data the only variable that i can input is the lastname. This is what the warning say employedata.c:7: warning: incompatible implicit declaration of built-in function
employedata.c:8: warning: incompatible implicit declaration of built-in function
#include %26lt;string.h%26gt;
#define MAX 5
void employedata(char lastname[30+1],char firstname[30+1],
float *hours,float *payrate,float *ssidefr)
{
int i = 0;
printf("Enter employee last name ");
scanf("%s", lastname[i]);
printf("Enter employee first name ");
scanf("%s", firstname[i]);
printf("Input employee hours ");
scanf("%f", hours[i]);
printf("Input employee payrate ");
scanf("%f", payrate[i]);
printf("Input deferred earnings ");
scanf("%f", ssidefr[i]);
}
Question on c I am using linux/unix to write my code?
It can't find the printf function. Add:
#include %26lt;stdio.h%26gt;
Reply:What exactly is the question ?
If you are wondering why you are getting warnings, add this in the beginning:
#include %26lt;stdio.h%26gt;
Reply:i think scanf expects a pointer, but you give him a char.
try this:
scanf("%s", %26amp;lastname[i]);
scanf("%s", %26amp;firstname[i]);
employedata.c:8: warning: incompatible implicit declaration of built-in function
#include %26lt;string.h%26gt;
#define MAX 5
void employedata(char lastname[30+1],char firstname[30+1],
float *hours,float *payrate,float *ssidefr)
{
int i = 0;
printf("Enter employee last name ");
scanf("%s", lastname[i]);
printf("Enter employee first name ");
scanf("%s", firstname[i]);
printf("Input employee hours ");
scanf("%f", hours[i]);
printf("Input employee payrate ");
scanf("%f", payrate[i]);
printf("Input deferred earnings ");
scanf("%f", ssidefr[i]);
}
Question on c I am using linux/unix to write my code?
It can't find the printf function. Add:
#include %26lt;stdio.h%26gt;
Reply:What exactly is the question ?
If you are wondering why you are getting warnings, add this in the beginning:
#include %26lt;stdio.h%26gt;
Reply:i think scanf expects a pointer, but you give him a char.
try this:
scanf("%s", %26amp;lastname[i]);
scanf("%s", %26amp;firstname[i]);
Subscribe to:
Posts (Atom)