I am trying to write a code that will take two intergers, and will list all numbers lower than both of them except for those that are a factor of either if the two numbers inputted. At some point in my code though (see below) i am getting an error saying expected expression. I am a beginner so if you could explain this too me as simply as possible.
int main(int argc, const char * argv[])
{
#autoreleasepool {
int firstInterger;
int secondInterger;
int i;
printf("Please enter the first interger: ");
scanf("%i", &firstInterger);
printf("Please enter the second interger:");
scanf("%i", &secondInterger);
for (i = 0; i < firstInterger && i < secondInterger; i++) {
if ((firstInterger % i !== 0) && (secondInterger % i !== 0)) { //ERROR HERE!
printf("%i", i);
}
}
}
return 0;
}
You have twice the same error in that line.
You should replace !== with != or ==
Related
I am trying to write a program that will take an integer input, and then convert it to words. for example: 123, one two three. Also -3908, negative three nine zero eight.
My code works 90% of the time, the only issue coming along when i am putting one or more zeros on the end of the integer. eg. 70800 will come up as seven zero eight. It completely misses the end zeros. I understand why that is happening but does anybody know if there is a way around it.
PS(i am not allowed as a part of this task to accept the input as a string and split it into an array, so it would be best if the answer is based off this code).
int main(int argc, const char * argv[])
{
#autoreleasepool {
float abNumber;
int i = 0;
float number;
float result;
float firstNumber;
printf("type a number: ");
scanf("%f", &firstNumber);
abNumber = abs(firstNumber);
if (firstNumber < 0) {
printf("negative ");
}
number = abNumber;
while (number >= 10) {
number = number / 10;
i++;
}
do {
float countNumber = abNumber;
float power = powf(10, -i);
float powerNo2 = powf(10, i);
countNumber = countNumber * power;
result = floorf(countNumber);
if (result == 9){
printf("nine ");
}
if (result == 8){
printf("eight ");
}
if (result == 7){
printf("seven ");
}
if (result == 6){
printf("six ");
}
if (result == 5){
printf("five ");
}
if (result == 4){
printf("four ");
}
if (result == 3){
printf("three ");
}
if (result == 2){
printf("two ");
}
if (result == 1){
printf("one ");
}
if (result == 0){
printf("zero ");
}
while (abNumber > powerNo2) {
abNumber = abNumber - powerNo2;
}
i--;
} while (i >= 0);
}
return 0;
}
The main error seems to be that
while (abNumber > powerNo2) {
should be
while (abNumber >= powerNo2) {
But I would recommend not to use floating point arithmetic at all, to avoid
possible rounding errors. The same can be achieved with simple integer arithmetic
(I have omitted the "negative case" for simplicity):
int number;
printf("type a number: ");
scanf("%d", &number);
// Determine highest power of 10 that is <= the given number:
int power = 1;
while (10 * power <= number) {
power *= 10;
}
// Extract each digit:
while (power > 0) {
int digit = (number / power) % 10;
/*
* Use switch/case to print 'digit' as a string ...
*/
power /= 10;
}
I would go for recursive solution, like that
int print(int num)
{
if( num )
{
int mod = num%10;
print(num/10);
switch(mod)
{
case 0:printf(" zero");break;
case 1:printf(" one");break;
case 2:printf(" two");break;
case 3:printf(" three");break;
}
}
return 0;
}
Recursivy divide the number untill nothing left of it, on the way back print the mod.
Why don't you just input the number as a string then loop through each character:
Exemple: http://ideone.com/E8QspN
Input:
-12003200
Output:
negative one two zero zero tree two zero zero
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
char input[25];
scanf("%s", input);
int i = 0;
while (input[i] != '\0') {
switch(input[i]) {
case '-' :
printf("negative");
break;
case '0' :
printf("zero");
break;
case '1' :
printf("one");
break;
case '2' :
printf("two");
break;
case '3' :
printf("tree");
break;
case '4' :
printf("four");
break;
case '5' :
printf("five");
break;
case '6' :
printf("six");
break;
case '7' :
printf("seven");
break;
case '8' :
printf("eight");
break;
case '9' :
printf("nine");
break;
default :
break;
}
printf(" ");
i++;
}
return 0;
}
Consider the following:
Code
#include <stdio.h>
#include <stdlib.h>
const char *numbers[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
void printNum(int num);
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%u", &num);
printNum(num);
printf("\n");
return 0;
}
void printNum(int num)
{
int absNum = abs(num);
if(absNum > 9)
printNum(num / 10);
if((absNum < 10) && (num < 0))
printf("negative");
printf(" %s", numbers[absNum % 10]);
}
Example Output
Enter a number: 2582
two five eight two
Enter a number: -943
negative nine four three
Enter a number: 1000
one zero zero zero
Enter a number: -1000
negative one zero zero zero
Logic
Get an integer from user.
Send to recursive function.
Keep recursing until the least significant digit is left. This is to print in the correct order.
Print digit as a string by using a lookup table.
Keep someone a high five.
To Do
Error checking
here is code.
int main(int argc, const char * argv[])
{
#autoreleasepool {
int x,y;
//BOOL divsibleYESOrNo;
NSLog(#"enter two number for test it\n");
scanf("%i %i",&x,&y);
if ( x%y ==0 ) {
NSLog(#"YES,it can be");
}
else if (x%y !=0) {
NSLog(#"no there cant.");
}
else
NSLog(#"zero is not allow");
}
return 0;
}
this code can not detect if the user input two zero.
how can I modify this code that can detect the input values are zero?
Thanks
Anything % 0 is undefined, so you'll need to add a check for y == 0 before your other if statements. It has to be the first if statement in order to make sure it gets evaluated; otherwise one of your other statements will catch it erroneously first.
if ( y == 0 ) {
NSLog(#"zero is not allowed.");
}
else if ( x%y == 0 ) {
NSLog(#"YES,it can be");
}
else {
NSLog(#"no there cant.");
}
const char *sentence = "He was not in the cab at the time.";
printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
int i = 0;
int countSpaces = 0;
while (sentence[i] != '\0') {
if (sentence[i] == 0x20) {
countSpaces++;
}
i++;
}
return countSpaces;
});
This code simply counts the white space in a string, but for some reason it says 1606416608 spaces rather than 8. I'm not exactly sure what is going wrong, so thanks for any help!
You're passing the actual block to printf, not the result of the block. Instead, try
const char *sentence = "He was not in the cab at the time.";
printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
int i = 0;
int countSpaces = 0;
while (sentence[i] != '\0') {
if (sentence[i] == 0x20) {
countSpaces++;
}
i++;
}
return countSpaces;
}()); // <-- note the extra parentheses here, indicating that you're calling the block
#include <stdio.h>
int main ()
{
FILE * pFile;
int n;
char name [100];
pFile = fopen ("myfile.txt", "w");
for (n = 0; n < 3; n++)
{
puts ("please, enter a name: ");
gets (name);
fprintf (pFile, "Name %d [%-10.10s]\n", n, name);
}
fclose (pFile);
return 0;
}
this code gives me a warning in gcc saying 'gets is a dangerous function to use'...is there a workaround for it?
Yes it is fgets. Replace your call to gets with:
fgets(name, 100, stdin)
For more details see the docs - the two are not exactly the same.
I want to use this code (from my last question (thanks Adam)),
bool AllDigitsIdentical(int number)
{
int lastDigit = number % 10;
number /= 10;
while(number > 0)
{
int digit = number % 10;
if(digit != lastDigit)
return false;
number /= 10;
}
return true;
}
but the compiler just says in the second line at } :
Nested functions are disabled, use -fnested-functions to re-enable
What can I do in my case? I have no plan…
Thanks and sorry for my bad English.
You wouldn't happen to have something like:
- (void) someMethod
{
bool AllDigitsIdentical(int number)
{
int lastDigit = number % 10;
number /= 10;
while(number > 0)
{
int digit = number % 10;
if(digit != lastDigit)
return false;
number /= 10;
}
return true;
}
}
That is, you have a function declared within a method's scope of implementation (though the same problem would occur for a function declared within a function).
In short, don't do that. It isn't supported and the means via which GCC implements it is considered to be a bit of a security hole (IIRC).
Move it outside:
bool AllDigitsIdentical(int number)
{
int lastDigit = number % 10;
number /= 10;
while(number > 0)
{
int digit = number % 10;
if(digit != lastDigit)
return false;
number /= 10;
}
return true;
}
- (void) someMethod
{
.... call the function here ....
}
this function looks fine, is it possible you missed a closing } in the code before this function ?
You probably pasted that into a method or into your main function! Make sure that that chuck of code is put OUTSIDE any other blocks of code.
so if you have main here:
int main(int argc, char **argv){
//blah
}
make sure that you put that code above or below it like so:
bool AllDigitsIdentical(int number){
//blah
}
do NOT put it inbetween the { or } of the main function (or any other method)