Try this
dd="20130409"
date -d $dd -d "next monday" +%Y%m%d
--ahamed
Try this
dd="20130409"
date -d $dd -d "next monday" +%Y%m%d
--ahamed
Check this
http://www.daniweb.com/forums/thread147417.html
and
http://www.daniweb.com/forums/thread148447.html
It should help you out
This thread is stupid.
ahamed101 -- save the data in files and fopen them according to the input parameters of the program.
OR do what you were doing with the #include and just live with code reuse.
I believe no questions are stupid. Please refrain from making such comments in the future.
@twomers
the first example is working fine.
"The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???
@gerard4143 : I can't use dlls, I have a restriction.
@Dave : The first example is working, the structure gets populated with values in the inc file.
Thanks for the replies.
Ok, if its not possible then is there any other way to populate this structure using the inc files without using #include?
Hi All,
Let me explain the problem.
I have multiple include files as in class1.inc, class2.inc, class3.inc etc. Contents of an include file will be like
class1.inc
{
"john",
12,
68,
"steve",
12,
98,
"mat",
12,
95,
};
This will basically serve as a static array of structures. Here there are three field name(char*), age(int), avg(float).
In my program I want to assign the value of one of these file to a structure variable. My code goes like this
struct std_
{
char name[50];
int age;
float avg;
}
std_str, *std_ptr;
int main(int argc, char **argv)
{
if(!strcmp(argv[2],"class1")
{
static std_str obj[200] =
#include "class1.inc"
...
}
else if(!strcmp(argv[2],"class2")
{
...
}
return 0;
}
The above code will work fine. But what I want is
int main(int argc, char **argv)
{
char file[50];
/* I want to dynamically generate the include file name and include it */
strcat(file, argv[2]);
strcat(file, ".inc")
static std_str obj[200] =
#include file
return 0;
}
But unfortunately, the compilation fails, saying #include expects a file name.
Is there anyway to achieve this?
Thanks and Regards,
Ahamed.
You know it’s love when you memorize her IP number to skip DNS overhead
If at first you don't succeed; call it version 1.0 -- i think we can call it beta version also right?? :D:D lol
nice ones...
You are right... "void * pointers says that it cannot be dereferenced"... I almost forgot that...
thanks...
Hi,
I googled and found this... Hope it is not an offence to place links to other forums for information...
http://ubuntuforums.org/showthread.php?t=381626
Check that it might help you... The actual issue is there are some invisible characters... Good Luck...
The same code compiled for me.
Platform HP-UX
Which is yours?
Hi All,
I am trying to write a common function called call which will take in a void* and an int type. Based on the int type it will have access to different structures. The code doesn't compile in the first place. I am not sure this is correct or not, cause I am accessing something from a void*. Can you please help me with some suggestions?
typedef struct a{
int x;
char y;
}as, *ap;
typedef struct b{
int m;
char n;
}bs, *bp;
void call(void *ptr, int i)
{
if(i == 1){
printf("\n x : %d", ptr->x);
printf("\n y : %c", ptr->y);
}
if(i == 2){
printf("\n x : %d", ptr->m);
printf("\n y : %c", ptr->n);
}
return;
}
int main(int argc, char *argv[])
{
as aobj;
aobj.x = 1;
aobj.y = 'A';
bs bobj;
bobj.m = 2;
bobj.n = 'B';
call(&(aobj),1);
call(&(bobj),2);
return 0;
}
Follwing is the error I get
error #2131: expression must have pointer-to-class type
printf("\n x : %d", ptr->x);
error #2131: expression must have pointer-to-class type
printf("\n y : %c", ptr->y);
error #2131: expression must have pointer-to-class type
printf("\n x : %d", ptr->m);
error #2131: expression must have pointer-to-class type
printf("\n y : %c", ptr->n);
Thanks in advance.
This part is wrong...
if (a==0 && b==0 && c==0) {
opselect = 1;
else if (a==0 && b==0 && c!=0)
opselect = 2;
else if (a==0 && b!=0 && c!=0)
opselect = 3;
else if (sqr < 0)
opselect = 4;
else if (sqr == 0)
opselect = 5;
else
opselect = 6;
}
The curly brace is the issue...
if (a == 0 && b == 0 && c == 0)
opselect = 1;
else if (a == 0 && b == 0 && c != 0)
opselect = 2;
else if (a == 0 && b != 0 && c != 0)
opselect = 3;
else if (sqr < 0)
opselect = 4;
else if (sqr == 0)
opselect = 5;
else
opselect = 6;
Having Chicken sandwich...
Awesome... :D:D:D
Dang, I must be noob - I don't know you but welcome back anyway.
Yeah me too... dont know you... welcome back anyways... :P
Yeah!!! True... the most complicated code we write should not be commented... It should be a piece of wonder and mystique for others...
Pal... put that while inside the for loop and not outside...
mmm... sorry... the place where we put is wrong... when the program is first executed there will not be anything in the buffer... so this will wait for something like that...
probably you can put that piece of code at the end of for loop... so that after everything is entered we can clear the buffer...
int main(){
...
for(){
...
while((ch = getchar())!= EOF && ch != '\n');
}
return 0;
}
Why do we have to clear the input buffer, cause there will be a '\n' in the buffer once you press "enter" after entering anything... so next time when scanf comes around looking for another character it will find '\n' in the buffer and takes it and goes happily with that... which we dont want... so just clear the input buffer...
welll sorry for it..
i need to figure how it's done...
this is a program that takes a measurement in one unit (e.g., 4.5 miles, etc) and converts it to another unit (i.e. kilometers).
the algorithm is
The algorithm for the program is fairly straightforward.
1
. Load units of measurement database.
2.
Get value to convert and old and new unit names.
3.
Repeat until data format error encountered.
4.
Search for old unit in database.
5.
Search for new units in database.
6.
If conversion is impossible, issue appropriate message,
7.
Else, compute and display conversion
8.
Get value to convert an old and new unit names..
and then the unit.dat
iskilometers km distance 1000 yards yd distance 0.9144 meters m distance 1 quarts qt volume 0.94635 liters l volume 1 gallons gal volume 3.7854 milliliters ml volume 0.001 kilograms kg mass 1 grams g mass 0.001 slugs slugs mass 0.14594 my code is this i
well how to search and display the error when have found the target??any ideas??
any ideas on the repeat data format until error found???
i have no idea...so and search for old and new units..
The code you have posted is full of syntax errors... correct it...
Meanwhile...
For repeating your code until the error is encountered you may use do-while or simple while …
Just clear the input buffer before you take the next input...
Do this...
char ch;
printf("Enter Birthday Details For User #%d? (Y/N): ",i+1);
while((ch = getchar())!= EOF && ch != '\n');
scanf("%c",&opt);
int years[SIZE];
int months[SIZE];
int days[SIZE];
The above variables are declared as arrays, they dont need to be arrays... simple variables would do...
int main(int argc, char **argv)
The return type of the main should be int... cause system expects a return value after executing your program...
int argc, char **argv -- you dont need them if you dont have anything to do with the command line arguments...
anyways--- argc gives the number of command line arguments and argv gives the value of the command line arguments...
First thing first...
You want to calculate the age of a particular user, why do want an array for that?...
Second...
/* Function Prototype */
int calAge(int currentYear[],int currentMonth[],int currentDay[],int birthYear[],int birthMonth[],int birthDay[])
/* Function Call */ calAge(currentYear[i],currentMonth[i],currentDay[i],birthYear[i],birthMonth[i],birthDay[i]);
The above function call and prototype does not match... In the function prototype, you have declared arrays as arguments... but in the function call you are not passing the arrays instead just a particular value(of an array)... this will surely make your program crash...
If all you want is to calculate the age of the person, just do it like this...
/* Function Prototype */
int calAge(int currentYear,int currentMonth,int currentDay,int birthYear,int birthMonth,int birthDay){
/* Put in your logic(existing one) without any array stuff */
}
/* Function Call */ calAge(currentYear[i],currentMonth[i],currentDay[i],birthYear[i],birthMonth[i],birthDay[i]);
Next...
/* Not a good idea */
void main()
/* Use this instead */
int main(int argc, char **argv)
Next...
/* This is wrong... */
scanf("%s",&name[i]);
/* You are taking in a string(array of chars), so no need of &, use it this way */
scanf("%s",name[i]);
Hi,
If you just want to read a string with space termination, you could just use scanf...
If you want to print the read characters, just find the length of the string read and print it...
You said the user input is arbitrary, in that case you can do
1) read each character and reallocate memory until enter is pressed
2) allocate certain chunk of memory and read the string, if the memory already allocated runs out, reallocate again...
In the above cases you can keep a track of the characters read...
If you are not sure about allocation and reallocation of the memory, please check here and here and here ...
There are examples at the bottom of how to use it...
Happy Coding...
It occurred to me that I might just increase the size of the chunk being reallocated, but burning questions arose: How do I determine what's a large enough size? I have trouble choosing things arbitrarily like this, and I begin to wonder whether reallocating single integers wouldn't affect my program at all? Why is this bad? What will happen?.
As you are the programmer, you should be knowing the initial size of the data that will be coming... Moreover, does the increase in data size constant?... or is it varying?... If its constant, then the job is easier... if its varying you might want to find the maximum possible size and allocate accordingly...
Anyways, whats the application all about?...
One thing is you need to reallocate memory dynamically... As you say calling realloc always for small data size ain't a good idea... In such a case you can allocate chunks of memory at time... like 32 bytes, 64 bytes, 128 bytes and so on... whenever your data exceeds the allocated memory, reallocate in these chunks... so that the call to realloc reduces and the memory allocation can be handled efficiently...
> better way...
Still two problems
- what happens at EOF
- the loop can exit with i == SIZE, which means your \0 is out of bounds.
Yes, EOF is an issue... but while entering the character where does EOF comes into picture?...
'\0' is out of bound when i == SIZE..
may be this can correct that..
while ((string[i++] = getchar())!='\n' && i < SIZE);
string[--i]='\0';
One more thing, with the previous code i.e. string='\0'... the i-1th position will be '\n' and ith position will be '\0' (which is of course out of bound of the array)... just for info...
I've been trying to read characters from the keyboard using 'getchar()' but my code always misses one character. Can anyone tell me why this is
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char string[50]; int i; printf("Enter a string: "); for (i=0; i < strlen(string); i++){ string[i] = getchar(); } printf("%s", string); }
This aint right...
for (i=0; i < strlen(string); i++){
string[i] = getchar();
}
strlen(string) will have some random number... you dont want that...
> printf("len = %d\n", strlen(string)); // len = 0
Nope, read my first post again.
string isn't initialised, so your length is junk.
If you get zero, that just makes you lucky.The rest is getting better though.
Salem is right...
String is uninitialised... you get junk for len... for sure...
You could do it like that:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 50 int main(int argc, char *argv[]) { char string[SIZE], c; int i; // just a test printf("len = %d\n", strlen(string)); // len = 0 printf("Enter a string: "); for (i=0; i < SIZE; i++){ c = getchar(); if (c == '\n'){ string[i] = '\0'; break; } string[i] = c; } printf("string entered = %s", string); }
This is good... but it will take one junk character... try executing this code... and its a bit lengthy code for such an read operation...
better way...
while ((string[i++] = getchar())!='\n' && i < SIZE);
string[i]='\0';
Just modifying Sci@phy's code...
while ((string[i++] = getchar())!='\n' && i < SIZE);
string[i]='\0';
This will do the trick...
did you include your .h file in your .c file?... yeah i know its a silly question, but check it, who knows thats the issue...
What is your problem anyways... No clue... seriously... one thing we know, you got a problem for sure... be more specific buddy...
Yes.. i saw that... May be before you're rude to people you should give some thought to the fact that programmers aren't the best when it comes to communicating and MAYBE the language they use is a bit obtuse for a greenhorn to understand.
It's quite easy being condescending when you've spent all your life learning things and the person you're talking to has only just begun... I wonder if you were talked to in this fashion when you first started out learning.In any case, thanks for your help. I hope it did your ego good to realise that, after years of studying and learning, you know more than a guy who's been programming for a grand total of 1 month.
Cheers,
Nishant
Please stop such talkings... Narue is far more experienced that you or me... No one has ordered him "Hey Narue, sit here and help this guy out"... he is volunteering... now that counts a lot... i mean it...
Moreover, have you tried to code it?... if so where is the code?... put in some effort... ohhh yeah I forgot, you are a newbie right?... well is there any rule telling "newbies need not put any efffort, come to DANIWEB and they will do the homework for you, huh?"... come on guys be realistic...
Anyways... Sorting an array in ascending order isn't much of an issue... you can use the qsort or else put in a simple logic like...
1. Take the …
This code will given an run time error only if you have miss "&" in the scanf section...Please check your actual code if you have the &A and not just A in the scanf section...
Hi,
For last one week I have been trying to do conversions between UTF8, Multibyte, Wide Character and Ascii... Finally managed to code a working program... Thought this might help some one, so posting it here... If there are any errors, please point out... If you have any doubts please ask...
#include <stdio.h>
#include <stdlib.h>
#include "locale.h"
#include "string.h"
#define WCMAXLEN 20
#define MBMAXLEN 10
int main(void) {
/* Temp Variable */
int iTemp = 0;
/* Choice of the user */
int iChoice = 0;
/* Length of the string */
int iLength = 0;
/* MB and WC string */
char acString[MBMAXLEN] = "";
wchar_t awArr[WCMAXLEN];
/* File Pointer */
FILE *pFile = NULL;
/* Pointer to character */
char *pcOut;
/* Loop Index */
int iLoopIndex = 0;
do {
/* Display the menu */
system("cls");
printf("\n 1. Japanese");
printf("\n 2. Ascii to Wide Character");
printf("\n 3. Ascii to UTF-8");
printf("\n 4. Wide Character to UTF-8");
printf("\n 5. UTF-8 to Wide Character");
printf("\n Enter your choice [0 to quit]: ");
/* Get the input */
scanf("%d", &iChoice);
switch (iChoice) {
case 1:
{
/* Multibyte to Wide Character */
/* Copy the japansese chracter */
strcpy(acString, "\x0e\x41\x71\x0f");
/* Set the locale and check the return value for error */
if (setlocale(LC_ALL, "ja_JP.utf-8") == NULL) {
printf("\n Locale failed");
}
iLength = strlen(acString);
/* Convert to side char */
iTemp = mbstowcs(awArr, acString, iLength + 1);
awArr[iLength + 1] = L'\0';
printf("\n Wide character string: %ls", …
this is wrong
#define SWS pRCC->CFGR, B2+B3, 2
do it like this, include the braces...
#define SWS (pRCC->CFGR, B2+B3, 2)
I take this back... I meant something else... Apologies guys...
I thought if there is space in the macro, the other arguments will be ignored and so I thought parenthesis was required... but thats not the case... I tried with space, its working...
this is wrong
#define SWS pRCC->CFGR, B2+B3, 2
do it like this, include the braces...
#define SWS (pRCC->CFGR, B2+B3, 2)
we are given the assignment to implement the banker's algorithm in c. this implementation must run in the linux terminal and program must be utilized the os in the time of execution. this means that it must capable on real-time execution with the os. i really don't have any idea how to do this. aside from that i am not that proficient in c, i'm more into java. can someone help me with this. any help would much appreciated.
Start coding and give it a try... we'll guide you if you get stuck... we will help you if you can't proceed... we will do everything to get you moving... thats what we do at DANIWEB...
1. take the first three numbers(which you have already done)
2. find their squares... a*a, b*b, c*c store them in the same variable
3. sum them, a+b+c
4. Divide by n
That simple... :)
Hi the code u posted is C code and not C++.
So u would get better help and more replies if u would post it in the right section.
As per you, it is C, then this is the right forum...
And what makes you say, its c++??...
Please find the comments inline
#include <stdio.h>
int main(int argc, char *argv[])
{
int g[] = {-1,1,3,7,2}; /* g is an array of integers */
int *x = g; /* x is a pointer to integer that points to the
array g(basically to the first element) */
int y;
y = ++(*x); /* y is now stored with the incremented value pointed by x
i.e. g[0]+1 = -1+1 = 0 */
x++;/* x now points to g[1] */
printf("%d %d",*x,y);/* will print g[1] as x points to g[1] and 0 */
y = *(++x); /* y is now stored with the value g[2] (which is 3),
++x increments the address and * gets the value */
x++; /* x now points to g[3] */
printf("%d %d",*x,y); /* will print g[3] and g[2] i.e. 7 and 3 */
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
char p[]="rain"; /* p is an array of characters storing the string "rain" */
char *t=p; /* t points to first char of array p which is "r" */
char *q=t-1; /* q points to a memory location just before the first character "r" */
while(*t) /* continue until t is null */
printf("%c %c\n",*(++q),*(t++));/* *(++q) => value of address+1 pointed by q.
*(t++) => value of address pointed by t and then increment the address pointed by t*/
return 0;
}
I am not that good at shifting operations... some one else can may be answer the first queries...
How do we link a .dll file?...
You can clear the input buffer also... use something like this...
char ch;
while((ch = getchar())!= EOF && ch != '\n');
I hope Aia doesn't shout at me for the code I have given here... :D... cause you all know there was a small talk over this code... who comes first, EOF or \n...
The bottom line is, it always works for me for clearing the input buffer...
Hi,
I want to read .wav file from the any directory in windowsXP and draw the graph of that particular file using C or C++ code.. Can anyone help please?????
How do you intend to do it?... Give us your logic and come code snippets...
I have not encountered a situation in which it copies even if the size is smaller...