It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
struct student
{
char am[7];/* èá íáé*/
float final_grade;
float exam_grade;
float essay_grade;
};
struct student mathites[1];
do{
printf("Dwse A.M.:");
scanf("%s",&mathites[j].am);
} while((mathites[j].am<8120000) || (mathites[j].am>812015));
I saw post like this, but everyone asks about comparison with "==" so the answer is strcmp,
something similar for this case?
something similar for this case?
This one is a little different since you're trying to check if a string is within a range of a numeric value.
A few points first:
struct student mathites[1];
if you're going to declare an array of one you may as well not make it an array.
char am[7];/* èá íáé*/
If the largest value you want to compare against is 7 characters "8120000" then you want your array sized 8 (to include space for the null terminator)
scanf("%s",&mathites[j].am);}
You don't need to use the & operator when getting a string, and you should use a limiter so you don't overflow your string
So taken this into account I'd say the code should be more like:
struct student
{
char am[8];/* èá íáé*/
float final_grade;
float exam_grade;
float essay_grade;
};
struct student mathites;
do {
printf("Dwse A.M.:");
scanf("%7s",mathites.am);}
Now your actual question, you need to convert the string to an number first, then check against the value:
long str_value = strtol(mathites.am, NULL, 10);
}
while((str_value <8120000) || (str_value >812015));
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am struggling with structures, and have a few questions that I can't figure out how to do.
first i had to define a structure call parts containing with int variable part_number,i and a character array with 25 characters.
second i have to define part to be a synonym for the struct part.
third read in part_number and a part_name from the keyboard into the individual members of variable a.
include <stdio.h>
int main(void)
{
struct part_containg
{
int part_number,i;
char part_name[25] !='\0';
// set up array of 25 characters and include terminating null character
};
struct part_containg part_number,part_number[i];
for(part_number=0; part_number<25;++part_number) // part_number goes up 1 for each part_name
{
printf("Intersert Part Name"\n);
scanf("%c", &part_name[i]); // scans for part_name
}
return 0;
}
You have quite a few syntax errors. You should consult an introductory text book: they are really helpful! I have read half a dozen and I would rate myself quite low on the ~"C Skill Continuum"~
Anyways, we all started at the bottom. Here is an alternate version with a lot of comments:
#include <stdio.h> /* you need a hash sign to include libraries...! */
#include <string.h> /* i use this library to copy input rather than loop */
int main() { // main does not take an argument
struct part_containing {
int part_number; // what is i for? I removed it
char part_name[25]; // do the null later, this is not the time!
};
struct part_containing name; // only declare the struct, not its variables
// p.s. name is an awful name-choice :P
char input[25]; // make a local var for input
puts("Enter part name:\n"); // prompt user for input
scanf("%s", input); // capture local var
strcpy(name.part_name, input); // copy it into the struct
name.part_name[25] = '\0'; // null just to be sure!
puts("Enter number:\n"); // same for the number now
scanf("%i", &name.part_number); // notice the syntax: &name.part_number...!
printf("%s and %i", name.part_name, name.part_number);
// simple error check
// did we get the expected input? It can be helpful to check in test runs!
return 0;
}
It does not answer all of the questions in your assignment, but it should be enough to get you started.
Hopefully this helps! If you work on it and have more questions then ask!
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
weather.outdooe_temp is a float value which is being updated every time I press a button. set_temp is a float to ascii function. If I use that the thing works, but not if I use the code below.
char Thermo_Buff66[4];
static void SetBox(ScreenObj_t const *pS, EVENT_MSG const *pMsg)
{
//set_temp(weather.outdoor_temp,&a);//it works if i use this function.
sprintf(Thermo_Buff66,"%2.1f",weather.outdoor_temp);
(void)sprintf(Thermo_Buff,"%s\xc2\xb0""",Thermo_Buff66);
(void)DataBoxHandler(pS, &msg, "Set Temp", (uint8_t *)Thermo_Buff);
//currently displaying any # value....!!ing!!
}
char Thermo_Buff66[4];
sprintf(Thermo_Buff66,"%2.1f",weather.outdoor_temp);
The buffer you have allocated (Thermo_Buff66) is too short for a floating number representing outdoor temperature (often 2 digits) plus a . plus a digit after. Indeed, it doesn't have space for the terminating '\0' character. So immediate correction would be to set the size to 5. Still, in case of armageddon (or simply being in a non-SI country ... cough ... US), the temperature could even get to above 100, in which case again you overflow your buffer. Do yourself a favor and use snprintf.
Regardless, you sprintf into a buffer, then using %s you sprintf it into something else, which there is no point to. You can do it all directly in one, removing Thermo_Buff66 altogether:
(void)sprintf(Thermo_Buff, "%.1f\xc2\xb0", weather.outdoor_temp);
(void)DataBoxHandler(pS, &msg, "Set Temp", (uint8_t *)Thermo_Buff);
Side note: the . and the precision digit already take up 2 characters. Setting minimum width to 2 is therefore reduntant. Perhaps you thought the 2 in %2.1 is the number of digits before the .? Well it's not. It's the minimum overall width.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a byte array (dB). I am trying to extract the bytes one by one. Why isn't this code working? Any pointers? Logically am I wrong? Or something wrong with my implementation?
You have byte buffers declared like this:
unsigned char *decodeBuf;
To read a single value from that buffer, at offset i you simply write:
unsigned char b = decodeBuf[i];
try
int main()
{
unsigned char tmp;
tmp = getByte(dB+dOffset); dOffset++;
}
it should work
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm trying to solve the 8th problem of the project Euler and I'm stuck because I can't manage to create a very long array of char.
There must be a stupid semantic issue, but I'm unable to find it.
char cifre[] = "very long list of numbers here";
Such example works with gcc:
#include <stdio.h>
int main (void)
{
char *x =
"73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450"
;
printf ("%s",x);
}
It prints your long number on screen without any problem. Symbol \ in string tells compiler that string literal is continued on next line. You are free to modify this example as you want. But please note that modifying content of string pointed by x isn't good idea.
Does adding a \ after each line of that 1000 digit number help?
It allows you to enter longer literals that span multiple lines.
Alternatively, surround each line of that long string in ", it will do the same thing.
As others have pointed out, you should probably allocate it dynamically. However, your question is somewhat vague as you didn't indicate where you are running into trouble or what your idea of "very long" is.
But here's some code to get you started:
#define ARRAY_SIZE 10240
char* pArray = (char*)malloc(ARRAY_SIZE);
memset(pArray, 0, ARRAY_SIZE);
free(pArray);