Monday, May 24, 2010

C++ little problem?

I'm having problem with this piece of code:





#include %26lt;stdio.h%26gt;





#include %26lt;conio,h%26gt;











void main( )





{





int y=0, r=0, c=8;





do{





clrscr( );





printf(“value:”); scanf(“%d ”, %26amp;y);





if (y = = 0)





r = r+1;





}while(y%26lt;=100);





printf(“ r = %d”, r);





}





****


ok, now my question is:


does this determine:


a) counts the number of zeros entered in n numbers that are lower than 101


b) counts the number of zeros entered in n numbers that are lower than 100





really having trouble with this, already did many tests but can't get to a conclusion, any help would be greatly appreciated.

C++ little problem?
Look at this from your program:





while(y%26lt;=100);





What this means is that the code in the loop will execute when the condition is true. In this case, the condition is that y is less than or equal to 100. So, when y is 100 the loop will still execute. But that is it. It will not work past 100. That means that it will count the numbers until it gets to 100 -100 will be counted.





So a) is the correct answer.
Reply:Choice a is the answer.
Reply:Entering in 100 would not get counted but the loop continues.





Entering 101 would not get counted and the loop would stop.





I would say both answers are correct. But, to stop the loop A would be the answer.





bad question though...if they are trying to teach while loop condition stops. Bad bad....


Why is there an error in C?

#include "stdafx.h





int main(int argc, char* argv[])


{


int c;





c = getchar();


while (c != EOF) {


putchar(c);


c = getchar();


}


}








That is my code. when I run it, it shows an error. I use Microsoft Visual C++, Windows XP, and I am making a Console Application. A little help please...

Why is there an error in C?
you are missing a closing quote on the include





#include "stdafx.h"
Reply:getchar(); is an undeclared identifier. What do you want to do with getchar(); ? What will getchar be? A number, a letter, a string, what? Identify it. Just like int c; you need to indentify getchar(); - Without quotations it becomes a variable, but the variable is undeclared. When in doubt, find out what type of program you are making and look for it on the internet. You can find alot of C++ programming examples. Just type around. You'll find the answer. In C++, one error leads to another. Tip: Build your application after each line of code so that you may find your errors as you go along. Instead you are writing a program with errors from the start. When you fix one error, another will pop up. Start over and build from beginning. Don't just code away.
Reply:You are missing a closing speech mark (") on your first line, so C thinks your whole file is part of the filename to include! (You are probably getting an EOF error).





Maybe this is more what you want (I'm assuming your header file stdafx.h contains the standard input/output library stdio.h):





---





#include %26lt;stdio.h%26gt;





int main(int argc, char* argv[])


{


int c;





while ( (c=getchar()) != EOF) {


putchar(c);


}





return 0;


}





---





You might also find this website useful (I do):


http://www.cppreference.com/index.html
Reply:lets see my answer is....................... get a life
Reply:I think you selected the wrong options while creating your project.





Why don't you try creating a new project and then copy and paste your main into the new project, and then try running it. ( and add the header )





The steps for creating a project are:


-New Project


-select C++ , win 32 on left side and Win 32 Console project on the right side


-select location where to create , give name


-select Application Settings, click on Console and Empty,


-Finish











Oh, and another thing is that you don't have a return statement in your main. You always need to have a return statement in your main function!!!! Since, you said int main, your main should return an integer and so you could put something like a return 0; as the last statement in your main.





Hope this fixes your problem.
Reply:! = i think





see what happens...





if not take the damn P.C. back to the shop and tell them you are to thick to own a p.c.!





LoL! I wish you luck... i have not got a clue... cheers!





I guess you got Cods Heads there! hope these kind people help!


Anyone have friends that they don't include in....?

Let's say your just friends with someone who you get along with. You've introduced this person to your group of friends. you and your friends like this person yet you never include them in your plans. Why??? Is it b/c you assume they already have friends to hang with (if so, did you ever bother to ask) Anyone ever done this???

Anyone have friends that they don't include in....?
People do that sort of thing because they are not open to having new friends since they got so close to other people. They feel like they have enough and they don't want to talk to other people, which is wrong, but that is the way it is. I can tell you are going through something like this, and you should ask to hang out, wether they like it or not.
Reply:Thank you!!! Hopefully you have made some friends! Report It

Reply:i have friends like that. i consider them accquaintences and i enjoy hanging out at school with them. i have about 4 main friends and the rest i usually just hang with at school


Help with c++ program?

#include %26lt;iostream.h%26gt;


#include %26lt;iomanip.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;fstream.h%26gt;


#include %26lt;conio.h%26gt;


using namespace std;





int main()


{


char ans[20]= {'B','D','A','A','


C','A','B',


'A','C','D','B','C','D','A','


D','C','C','B','D','A'};


char studentans[3][20];


int identity[3][20];


int totalwrong[3];





for(int count1 = 0;count1%26lt;3;count1++)


{


for(int count2 = 0;count2%26lt;20;count2++)


{


do


{


cout%26lt;%26lt;"Person #"%26lt;%26lt;


(count1+1)%26lt;%26lt;" question #"


%26lt;%26lt;(count2+1)%26lt;%26lt;"- ";


cin%26gt;%26gt;studentans


[count1][count2];


studentans


[count1][count2] = toupper


(studentans[count1][count2]);


}


while(studentans[count1]


[count2] != 'A' %26amp;%26amp;


studentans[count1]


[count2] != 'B' %26amp;%26amp;


studentans[count1]


[count2] != 'C' %26amp;%26amp;


studentans[count1]


[count2] != 'D');


}


}

Help with c++ program?
The only problem is you aren't initializing the totalwrong array. Do this before you use it:





memset(totalwrong,0,sizeof(totalwrong)...

nil

C loop, giving me trouble !! Can anyone help..?

i have this code to test for palindromes, it functions perfectly well on the first test, but as the program continues to check for more after the first word it keeps printing not palindrome, once it wasnt the first word typed, run it pls, and you'll c what im talking about. Can anyone help me fix it..?








#include %26lt;stdio.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;string.h%26gt;





int isPalindrome( char *s);


int main (void)


{


int i=0;


int ch;


char s[100];


while (1){


while ((ch = getchar()) != ('\n')){


if (isalpha(ch)){


ch=toupper(ch);


s[i]=ch;


i++;


}


}


s[i]='\0';


if (isPalindrome(s) ==1){


printf("is palindrome\n");


}


else{


printf("not palindrome\n");


}


system("Pause");


return 0;


}

C loop, giving me trouble !! Can anyone help..?
What you are doing when you pass a char * into a function is essentially passing a pointer to the first char in the array.





This will always start with the first word you enter, and since you are storing all the strings on the same array, there is no way to delineate the different strings.





reseting the i to variable to zero after every call to isPalindrome()
Reply:You need a way to break out of your while(1) loop. Otherwise, you'll just keep asking for chars over and over again.





One way to do this is that if getchar() == \n you can call break;, which will break the outer loop.





A nicer way is to set a flag = true before you enter the main loop, and use while(flag)





then if the character is a carriage return, set flag to false, and the loop rops out naturally.
Reply:You need to initialize your index i in the loop:





while (1){


i = 0;


while ...





You'd be wise to ensure the user doesn't enter more than 100 characters, causing you to overrun your array s[100]. If this is just a toy program for an assignment, it may not matter. Still, it's good to get in the habit of writing robust code.


C++: what command to exit program?

In c++, what command can exit the program? i tried adding #include %26lt;stdlib.h%26gt; and void exit(); but nothing happened.

C++: what command to exit program?
Please try this instead:





exit(1) // Returns 1 to the operating system
Reply:Just to clarify, you use the exit(%26lt;number%26gt;) as the other answerers have said. The %26lt;number%26gt; represents whether or not the program encountered an error, and the error is the reason for exiting. If you use exit(0), there has been no error. Using exit(1) indicates there has been an error. Report It

Reply:Good point. Thx. Report It

Reply:exit(0);





It is used to exit ur c++ program


C code problem?

/* Sendind and receving values between functions */


#include%26lt;stdio.h%26gt;


void calsum();


void main()


{


int a,b,c, sum;


printf("\n Enter any three numbers");


scanf("%d %d %d",%26amp;a, %26amp;b, %26amp;c);


sum= calsum(a,b,c);


printf("\nSum=%d,sum");


}


void calsum(x,y,z)


int x,y,z;


{


int d;


d=x+y+z;


return (d)


}


This code is giving errors


So where I am wrong

C code problem?
Nithinpb is correct. However, this is one more thing. When you declare a function and you are writing out the actual code for the function, you need to give the passed values a data type. So it needs to be:





int calsum(int x, int y, int z)





Also, you need to declare "int d" outside the function, otherwise it will move out of scope once you leave the function and the program won't have a variable d (because you only declare the "int d" in the function, not in the main).
Reply:It should not be void calsum(x,y,z). You are not returning any value. In the main you are expecting a value. So, its giving error.





Make it, int calsum(x,y,z).





--


Nithin.


http://www.bluminut.com
Reply:You'll also get an error on the line with printf("\nSum=%d,sum"); It should be printf("\nSum=%d",sum); Your 2nd quotation is in the wrong place.





In the future, let us know what the error says and we can give better feedback.
Reply:void calsum(x,y,z)


int x,y,z;


{


int d;


d=x+y+z;


return (d)


}





i think this line is the one giving errors because x, y, z are parameters and used without being declared, try this:





void calsum(int x,int y, int z)


{


int d;


d=x+y+z;


return (d)


}