You define your function header like
void product (float x, int y, float *prd)
and
void product (float, int, float)
Which are different.
You define your function header like
void product (float x, int y, float *prd)
and
void product (float, int, float)
Which are different.
I would try printing the value of file1, just to see what the function is returning.
Why do you have these lines if your using command line agruements
to pass the values?
printf("Enter the name of the file you want to back up.>");
scanf("%s", argv[0]);
printf("Enter the name of the file to back up to.>");
scanf("%s", argv[1]);
Don't request everything in one scanf call. Call scanf for your sentinel value the check the result and then call scanf for the remainder.
Here's a working copy of part of your program...You really should write your programs in small sections. That way you can get one section working and then move onto another..Note I went with char pointer instead of char array for letter.
Please also note the formating of the program...It much easier to read.
Things like a for statement for(i=0;i<N;i++) with no spaces should be avoided.
#include <stdio.h>
#include <math.h>
#define N 2
#define ISIMUZUN 15
struct student
{
char name[ISIMUZUN];
char surname[ISIMUZUN];
int number;
int final;
int midterm;
int hw1;
int hw2;
int grade;
char *letter;
};
int main(int argc, char *argv[])
{
struct student info[N];
int i = 0;
for(i = 0; i < N; i++)
{
printf("Please enter student's information with blanks between them\n");
scanf( "%d %s %s %d %d %d %d", &info[i].number, \
info[i].name, info[i].surname, &info[i].midterm, \
&info[i].final, &info[i].hw1, &info[i].hw2);
info[i].letter = "AA";
}
for(i = 0; i < N; i++)
{
printf ( "%d %s %s %d %d %d %d %s\n", info[i].number, \
info[i].name, info[i].surname, info[i].midterm, \
info[i].final, info[i].hw1, info[i].hw2, info[i].letter);
}
return 0;
}
Your trying to assign a const string pointer to a char [3] with this line which is a no no.
info[i].letter= "DD";
Try something like
info[i].letter[0] = 'A';
info[i].letter[1] = 'A';
Your scanf should be reading the data into the address of the structure members..
&info.number
I couldn't figure out how to do it with multiple students. For example, for 6 students how can i organize my struct?
struct student 0
....
....
....struct student 1
....
....
....
.
.
.
struct student 5
....
....
....That's why I used struct variable inside for loop. I know it is so amateur but i could not figure out how to get what I desire.
If you want an array of structures then define your variable like this:
struct student i[6];
Here's a link on structures in C
http://www.exforsys.com/tutorials/c-language/c-structures-and-unions.html
Hello, there.
I'm trying to write a program which reads student information from keyboard and does some calculations. I've just started it and I dont know much about this "Struct" topic. As far as I know, I've tried to do something and here is the result.#include <stdio.h> #define N 2 #define ISIMUZUN 15 struct student{ char name[ISIMUZUN]; char surname[ISIMUZUN]; char number[10]; int final; int midterm; int hw1; int hw2; }; int main(int argc, char *argv[]) { struct student i; for(i=0; i<N; i++) { printf("Please enter student number %d information with blanks between them\n", i+1); scanf( "%s %s %s %d %d %d %d", i.number, i.name, i.surname, i.midterm, i.final, i.hw1, i.hw2 ); printf("%s %s %s %d %d %d %d\n", i.number, i.name[ISIMUZUN], i.surname[ISIMUZUN], i.midterm, i.final, i.hw1, i.hw2 ); } system("pause"); return 0; }
I'm getting several errors while compiling this. Does anyone have an idea about whats wrong with it?
Regards..
Why are you using your struct variable as a counter in your for loop?
This client/server program...Windows or Linux?
In your client, try shutting down the write portion of the client socket after you sent the filename to the server.
Also try downloading wireshark, Its great for debugging client/server programs.
Gerard -- that is assuming the varible being received and passed are the same, right? In my case, that isn't the case. I'm passing one element of a structured array ([x].input), but I'm receiving a completely different array ([x].input_binary[]).
This was an example of passing the array. If you want to pass both, then create a function with two parameters.
To pass the array try something like below..
#include <stdio.h>
#define ARR_SIZE 8
void myfunc(int *myint)
{
unsigned int i = 0;
for (i = 0; i < ARR_SIZE; ++i)
myint[i] = i + 5;
}
int main(int argc, char**argv)
{
unsigned int i = 0;
int array[ARR_SIZE];
myfunc(&array[0]);
for (i = 0; i < ARR_SIZE; ++i)
fprintf(stdout, "array[%u]->%d\n", i, array[i]);
return 0;
}
Thank you for your reply. I didn't know what the problem was, thank you for explaining. I DO know that it doesn't work.
Do you have any suggestions on what to change?
You could pass the array to the function as a parameter.
OK. Thank you. I'll go ahead and write a main() function to produce some sort of output for debugging purposes. In the first two functions I wrote, I did write, save, compile, and debug each one before moving to the next one. The third function is where I ran into the error I could not see (though it was a simple error of the extra ',' and the ';' at the end of the function. Currently, I am sorting out some redundant variables and the variable I named that shadows the parameter in the call (more aggressive attention to detail needed on my part). I appreciate the advice so far with this. Do you have a recommendation or advice on how you would approach the comparison in line 89? I don't mean this insultingly or harshly, but your answer of "No, I would not make that comparison" was a little ambiguous, but then again, it WAS a direct answer to my question as well.
I am trying to not post the code redundantly as I appreciate the back and forth conversation that lets me figure the issues out on my own. However, an example of how you would perform the comparison in line 89, or a pseudo-code variation would be most appreciated.
When a programmer checks for EOF, its usually done as part of the read/get...
#include <stdio.h>
int main(int argc, char**argv)
{
unsigned int ch;
while ((ch = getc(stdin)) != EOF)/*part of the read/get*/
{
fputc(ch, stdout);
} …
Hi. I'm trying to call a function to convert a decimal integer into a binary number (stored as an array of integers). Before I knew that I couldn't return an array, this is how I thought to implement it. Can anyone help me understand the way I'm actually supposed to do this?
Thank you very much.
Here is MAIN:#include <stdio.h> #include <math.h> int dec2bin (int decimal); int main(void) { int binary[8]; // creates array to store 8-bit binary number int decimal=5; // with a test value. final version will have user input. binary = dec2bin(decimal);
And my function:
int dec2bin (int decimal) { int binary[8]; int x; for (x=8; x>0; x--) { binary[x] = decimal % 2; decimal /= 2; printf("%d", binary[x]); } return binary; }
Your returning an array that's created on the stack...This is never a good idea because when the function returns it releases its stack memory....therefore invalidating it(the array binary[8]).
When your new to C programming you should think of programing as set of steps and smaller the steps the better.
Program a step, compile and fix all errors and warnings. If possible try running the program. Does it produce correct results? If not - correct and recompile.
As a novice you should never get into a situation where you have page(s) of code which has not been compiled and run...Actually this advice applies to all programmers not just novices
As for your if statement...No I won't make that comparison.
int max_temp(measured_data_t id, measured_data_t temperature, , int *ID, int *max_temp);
You have two , , with no parameter. Probably should be
int max_temp(measured_data_t id, measured_data_t temperature, int *ID, int *max_temp);
You really shouldn't mix cout and printf especially on the same line...When you have this
<<printf("whatever")
You'll print out the result of calling printf which is four characters in your code.
Because I haven't read your materials I can only guess at what you require but the basic setup of your program is wrong...or at least inconsistent...I think this is what your after.
#include <stdio.h>
#include <string.h>
#define ARR_SIZE 4
typedef struct
{
int site_id_num;
int wind_speed;
int day_of_month;
int temperature;
} measured_data_t;
int main(void)
{
int i = 0;
measured_data_t thestr[ARR_SIZE];
for (i = 0; i < ARR_SIZE; ++i)
{
printf("%-10s%-6s%-17s%-20s", "ID", "Day", "Wind Speed (knots)", "Temperature (deg C)");
fscanf(stdin, "%d%d%d%d", &thestr[i].site_id_num,\
&thestr[i].day_of_month,\
&thestr[i].wind_speed,\
&thestr[i].temperature);
}
for (i = 0; i < ARR_SIZE; ++i)
{
fprintf(stdout, "our data - id->%d, day->%d, wnd->%d, temp->%d\n", thestr[i].site_id_num,\
thestr[i].day_of_month,\
thestr[i].wind_speed,\
thestr[i].temperature);
}
return 0;
}
I say this is the most helpful way...Get an intro book on C programming and read. You have so many errors in your code that I'm not sure where I should begin...
Your structure. Why do you have integer arrays for things like site_id_num, wind_speed, day_of_month and temperature?
If your using fscanf then it should read like
fscanf(stdin, "format string", ....);
Are you really sure you want your while to read like below? Maybe you mean while (true) or while(1)
while ('\n')
If the compiler will generate the proper machine codes and the linker will produce the proper addresses then yes it will produce executable code....The only remaining obstacle is wrapping the executable code in a format recognized by the operating system...
Did you try investigating setjmp, longjmp.
Also sometimes, for efficiency, goto's are the best solution.
Here's what manpages has to say about return values for fcsanf()
RETURN VALUE
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in the
event of an early matching failure.The value EOF is returned if the end of input is reached before either
the first successful conversion or a matching failure occurs. EOF is
also returned if a read error occurs, in which case the error indicator
for the stream (see ferror(3)) is set, and errno is set indicate the
error.
Try running this code...
#include<stdio.h>
struct converter {
unsigned short int i;
};
main() {
struct converter * converts;
char a[9]="12345678\0";
converts = (struct converter*) (a + 0);
printf("12 is printed %p\n", ntohs(converts->i));
}
it produces - 12 is printed 0x3132
Where 0x31 and 0x32 are the ascii values of 1 and 2
Also manpages has this to say about ntohs
htonl, htons, ntohl, ntohs - convert values between host and network
byte order
Hi all,
the following code prints garbage values. I want to read from an array two bytes and print then as a short int after doing ntohs.
What is the bug in the code?Thanks and Regards,
Prashanth#include<stdio.h> struct converter { unsigned short int i; }; main() { struct converter * converts; char a[9]="12345678\0"; converts = (struct converter*) (a + 0); printf("12 is printed %u\n", ntohs(converts->i)); }
You realize that ch[9] is an array of characters not integers.
cvs files are comma delimited right? You may have to set the sort delimiter with -t
I am nt getting the desired out put here! I am still getting the duplicates...
my requirement is copare each and every first filed..
when i executed this i am getting three records:
awk 'x[$1]++ == 1 { print $1 " is duplicated"}' inputfile.cvs
is there anyway to compare only first field and print full data...
That's because awk is reading the file. You need to have sort read the file and pipe the results to awk...Like I showed you in the above postings.
Actually, I am new to shell scriting.
you mean to say:
sort -u | awk '!x[$0]++' $1 > results-new.cvs
Almost $1
belongs with the sort command
sort -u $1 | awk '!x[$0]++' > results-new.cvs
I never used VB script. It looks extravagant.
The worst thing you can do while bash shell scripting is think like a 'traditional' programmer. Things are done differently in bash scripting.
Like this..
#!/bin/sh
if [ $# -ne 1 ]
then
echo "Usage - $0 file-name"
exit 1
fi
touch results.cvs
if [ -f $1 ]
then
echo "$1 file exist"
sort -u $1 | awk '!x[$0]++' > results.cvs
else
echo "Sorry, $1 file does not exist"
fi
Note - You really should have the proper exits in your program.
I have file which is having large data in it. and there are some repeated rows. Basic idea here is : Sort data, remove duplicates based on first field and then print the whole....
I have tried teh following but no help..
#!/bin/sh if [ $# -ne 1 ] then echo "Usage - $0 file-name" exit 1 fi if [ -f $1 ] then echo "$1 file exist" sort -u $1 > results.cvs awk '!x[$0]++' results.cvs > results-new.cvs else echo "Sorry, $1 file does not exist" fi
Input data and expected out put data Attached :
trying in HP-UNIX
Actually looking at your script, you have sort...
Why are you using a temp file results.cvs. Won't it make more sense to pipe the result of sort directly into awk.
I would use the sort utility for this one..
I wrote this simple program to demonstrate how the exec family of functions behave:
testp.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char**argv)
{
execlp(argv[1], argv[1], NULL);
fprintf(stdout, "failed\n");
exit(EXIT_SUCCESS);
}
Usage: execute program with a valid shell executable say ps
./testp ps
Then execute with a bogus shell executable say pssssp
./testp pssssp
does it print "program done" and return 0?
Which one..The program forks
Here's what the manpages have to say...
RETURN VALUE
If any of the exec() functions returns, an error will have occurred.
The return value is -1, and errno will be set to indicate the error.
I would check the number of } braces you have in your function definition.
Also calling a function print is probably a bad idea, considering you already have fprintf, printf, sprintf, vprintf, ... defined in the standard library.
Why these lines?
#include <iostream>
using namespace std;
I am not sure what this means and I get it all the time.
Really and what would that be?
Can someone help me fix this code?
What's wrong with it beside not having code tags?
Hi,
I was looking at this tutorial:http://linuxgazette.net/77/krishnakumar.html
There's an instruction there that says "seg es", my question is whats this instruction in AT&T style? I'm trying to rewrite the code in AT&T syntax and use it with gcc/ld but it gives me an error: "kernel.s:6: Error: no such instruction: `seg %es'"
Any help appreciated!
Best regards,
Keith
I won't go by that syntax if you using GCC/AS..
This about the same thing
.code16
.section .data
.scetion .text
.global _start
_start:
movw $0xb800, %ax
movw %ax, %es
movb $0x41, %es:0
movb $0x1f, %es:1
movb $0x34, %es:2
movb $0x1f, %es:3
movb $0x31, %es:4
movb $0x1f, %es:5
movb $0x34, %es:6
movb $0x1f, %es:7
movb $0x33, %es:8
movb $0x1f, %es:9
loop1:
jmp loop1
Is this the complete program?
Try this instead
fgets
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.
#include <stdio.h>
int main()
{
char ch[3];
fputs("enter two characters->", stdout);
fgets(ch, 3, stdin);
printf("%c %c\n", ch[0], ch[1]);
return 0;
}
Most/all C/C++ compilers have a swtich to allow you to view the assembled code
gcc's switch is -S
`-S'
Stop after the stage of compilation proper; do not assemble. The
output is in the form of an assembler code file for each
non-assembler input file specified.
Are you talking about a variable length argument list? If you are then google "C variable length argument list".
By stop do you mean the program exits?
You really should try running your code with a debugger...
If you not comfortable running a debugger then place a printf right after the scanf
and check the value of max
printf("Enter an integer between 2 and 50,000: ");
scanf("%ld", &max);
printf(check value of max);
why do you define your int like this
long int &max;
with the "&"...
Don't you mean
long int max;
Why can't you send integers with write....
int myint = 1234;
write(fd, (char*)&myint, sizeof(int));
How do I convert it into a Qemu boot image?
If you compiled it correctly then it should be ready to go...I used gcc/Gas, a linker script and a hexeditor/objdump to compile and then strip my bootimage but with Nasm the functionality should be wrapped up in the compile/link lines.
I would check the Nasm docs to make sure your compiling/linking correctly and then download/install qemu. After you accomplished that open a console window go to your working directory(the one with your boot file) and type qemu bootimage..
Note I'm assuming you called your boot file bootimage...