Structures , and defining a variable as synonym [closed] - c

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!

Related

Forbids comparison between pointer and integer [closed]

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));

How to create separate entities from strings in a text file in C? [closed]

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.
I'm looking for a solution to a rather simple problem:
Have a text file with words\strings separated by '\n' Each new string starts after '\n'
Need to get each one of those strings out of the file and create a separate entity out of it.
Yes, this task sounds pretty straightforward except for the vague idea of "creat[ing] a separate entity" out of a string (???).
Here is the general algorithm that I would use if I were to implement such a program:
#include <stdio.h>
#define LEN 0xFF
int main(int argc, char ** argv) {
char line[LEN];
FILE * input_file = NULL;
if ((input_file=fopen("myinputfile.txt","r")) == NULL) {
// Handle error when opening the file...
}
while (fgets(line, LEN, input_file) != NULL) {
// Process each line of input...
}
fclose(input_file);
return 0;
}
Based on the vague description that you gave, what you can do is to use an array (buffer), and in conjunction with a fopen, a loop, and sscanf, you can match the results of the file and put them into separate elements within the array. Then you can access the results. You'll likely need to track some indices.
There's a bunch of other ways that you can do this, but more information is definitely needed.

How can I write a program that transforms letters to upper or lower case depending on command line arguments? [closed]

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.
I was writing this code for a C program that transforms letters to lower case when called as:
./arg lower
and transforms them to upper case when invoked as:
./arg upper
Here is the code which I wrote, kindly help me out:
#include<stdio.h>
#include<ctype.h>
main(int argc,char *argv[])
{
int i;
char c;
for(i=1;i<argc;++i)
{
if(*(argv+i)=="lower")
{
while((c=getchar())!=EOF)
{
c=tolower(c);
putchar(c);
}
}
if(*(argv+i)=="upper")
{
while((c=getchar())!=EOF)
{
c=toupper(c);
putchar(c);
}
}
}
return 0;
}
You can't do string comparisons like that in C, you need to use strcmp(3).
Better yet, use strncmp(3) and specify the size of your buffer.
Finally, for array access, you could use argv[i] instead of *(argv+i). The meaning is the same, but the first version is much more readable.
So, you'd want something like:
if (0 == strncmp(argv[i], "lower", 6)) {
...
}
You have to use the strcmp() function to compare strings.
When doing *(argv+i)=="lower" you are actually comparing the pointer to such c-strings. You need to use strcmp to see if the content of such strings are equal, in which case strcmp returns zero.
fgetc returns an int, not a char. If you truncate it you'll be ok this time, but will suffer horrible torments when working with binary data.

error in following c code [closed]

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.
Note: The question has been edited to make the problem go away.
I have written this code to reverse an array using functions. But there is an error in line 24 saying ' ) expected'. I have read it again and again but i couldn't find the error. Can anybody please reveal it and tell me how to remove it?
#include<stdio.h>
#include<conio.h>
#define max 5
/*function prototype*/
void reverse(int[],int);
void main()
{
int arr[max]={1,2,3,4,5};
int i,j;
clrscr();
printf("the list before reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
reverse(arr,max);
printf("\n the list after reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
getch();
}
/*function for reversing elements of array*/
void reverse(int num[],int max)
{
int i,j,temp;
for(i=0,j=max-1;i<max/2;i++,j--)
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
max is defined as a macro. So after preprocessing it becomes
void reverse(int num[],int 5)
Which is not valid and you are getting ' ) expected'. If max is constant then there is no need to pass it as a parameter. And also you have a missing for in the function.
You seem to be missing the for keyword in the loop header in reverse().
EDIT:
Ok, ok my first answer was stupid...
(Answered without thinking enough)
Now I got it:
The problem is
#define max 5
Later max (=5) is used as a parameter!

Array of a very long list of ints [closed]

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);

Resources