Remove first two characters from char* or from int?


I have a char *year that contains a 4-digit year (i.e 1929), I use the
following syntax c = atoi(year); to convert it to an integer. However
I must strip the first two numbers. I've tried to convert it from
char* to a string and use the erase function, which works, but then I
can't seem to convert the string it to an integer. I am impartial as
to when I remove the first two digits, meaning it can be done before
or after the conversion to int. In my example above the end result
should be an integer variable equalling 29.

, yogi_bear_79 wrote:
>> c = atoi(year+2);

> Perfect!
Posted On: Sunday 4th of November 2012 10:25:14 PM Total Views:  230
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




How to remove Segmentation Fault

The following code can detect a (single) macro name (stored in str1) from a file and store its definition (stored in str2) and then reads the file again to write into a new file, replacing all occurrences of the macro name with its definition. The code when executed gives segmentation fault, please help me remove it. the mac() is working properly, there's a problem with the main(). Please help. Code: #include #include #include FILE *fp1, *fp2; int i; char str[10]; char str1[10],str2[10]; void mac (void) { char ch; fp1=fopen("comm.txt","r"); do{ ch=fgetc(fp1); if(ch!='\n') { while(ch!='\n') { ch=fgetc(fp1); } } ch=fgetc(fp1); if (ch=='#') { ch=fgetc(fp1); if(ch==' ') { i=0; ch=fgetc(fp1); while (ch!=' ') { str1[i++]=ch; ch=fgetc(fp1); } str1[i]='\0'; } //puts(str1); if(!(strcmp(str1,"define"))) { i=0; ch=fgetc(fp1); while(ch!=' ' && ch!='\n') { str2[i++]=ch; ch=fgetc(fp1); } str2[i]='\0'; } } } while(ch!= EOF); fclose(fp1); } int main(void) { char ch; int flag=1; fp1=fopen("comm.txt","r"); fp2=fopen("new.txt","w"); mac(); do{ if(flag==0) { ch=fgetc(fp1); } if (ch!='#') { while(ch!='\n' && ch!=';') { i=0; while(ch!=' ' && ch!='\n' && ch!=';') { str[i++]=ch; ch=fgetc(fp1); } str[i]='\0'; //ch=fgetc(fp1); if (!(strcmp(str,str1))) fputs(str2,fp2); else fputs(str,fp2); fputc(ch,fp1); ch=fgetc(fp1); } fputc(ch,fp1); if (ch=='\n') flag=1; else flag =0; } else { while(ch!='\n') { fputc(ch,fp2); ch=fgetc(fp1); } fputc(ch,fp2); flag=1; } }while(ch!=EOF); fclose(fp1); fclose(fp2); return EXIT_SUCCESS; }
VIEWS ON THIS POST

162

Posted on:

Wednesday 19th September 2012
View Replies!

Compare two string and remove common letters

Helo Cprogramming members, I am new to programming. I started lessons at c in my university and i am having trouble with an exercise. Ok here is what exercise asks for: "Write a program that aids with appropriate messages on the screen to read two character strings str1 and str2 even which will be given from the keyboard and then delete all the letters in the variable str1, which appear in the variable str2. The display shows the final result for checking the correct operation of the program." For example: string1: eleos string2: lo final result: e e s Here is what i have done, but its not working properly.. Note that i can use only that libraries. Code: #include #include #include int main() { char str1[80], str2[80]; int megethos1, megethos2,max,i,j; printf ("Give first string: "); scanf ("%s", &str1); printf ("Give second string: "); scanf ("%s", &str2); size1= strlen(str1); size2= strlen(str2); for (j=0; j
VIEWS ON THIS POST

171

Posted on:

Saturday 6th October 2012
View Replies!

How to remove Segmentation Fault

The following code can detect a (single) macro name (stored in str1) from a file and store its definition (stored in str2) and then reads the file again to write into a new file, replacing all occurrences of the macro name with its definition. The code when executed gives segmentation fault, please help me remove it. the mac() is working properly, there's a problem with the main(). Please help. Code: #include #include #include FILE *fp1, *fp2; int i; char str[10]; char str1[10],str2[10]; void mac (void) { char ch; fp1=fopen("comm.txt","r"); do{ ch=fgetc(fp1); if(ch!='\n') { while(ch!='\n') { ch=fgetc(fp1); } } ch=fgetc(fp1); if (ch=='#') { ch=fgetc(fp1); if(ch==' ') { i=0; ch=fgetc(fp1); while (ch!=' ') { str1[i++]=ch; ch=fgetc(fp1); } str1[i]='\0'; } //puts(str1); if(!(strcmp(str1,"define"))) { i=0; ch=fgetc(fp1); while(ch!=' ' && ch!='\n') { str2[i++]=ch; ch=fgetc(fp1); } str2[i]='\0'; } } } while(ch!= EOF); fclose(fp1); } int main(void) { char ch; int flag=1; fp1=fopen("comm.txt","r"); fp2=fopen("new.txt","w"); mac(); do{ if(flag==0) { ch=fgetc(fp1); } if (ch!='#') { while(ch!='\n' && ch!=';') { i=0; while(ch!=' ' && ch!='\n' && ch!=';') { str[i++]=ch; ch=fgetc(fp1); } str[i]='\0'; //ch=fgetc(fp1); if (!(strcmp(str,str1))) fputs(str2,fp2); else fputs(str,fp2); fputc(ch,fp1); ch=fgetc(fp1); } fputc(ch,fp1); if (ch=='\n') flag=1; else flag =0; } else { while(ch!='\n') { fputc(ch,fp2); ch=fgetc(fp1); } fputc(ch,fp2); flag=1; } }while(ch!=EOF); fclose(fp1); fclose(fp2); return EXIT_SUCCESS; }
VIEWS ON THIS POST

121

Posted on:

Tuesday 16th October 2012
View Replies!

remove redundant white spaces in string

is a strange request. I don't need a code. could somebody please explain me how it has to be done step by step. I just need the logic in for task. , lets say i have a string Code: "this is a string with redundant spaices in it" I don't have any algo in my mind yet, O, I see. My bad. I meant if more than one white space among two words = redundancy. Answer A in your assumptions , damn what I have done to curse me like that Anyway thank you for hint and help., But I did exlained so you can see. Look at your post and examples. Which particular example you typed first , We are all getting stressed from time to time, like me now. I just don't follow in this part of pseudo code. Code: r = 0//got it w = 0/got it while string[r] is not a null character // that is ok string[w] = string[r]//yep all the steps to copy one increment r//string if string[w] is a white space character // right, lets say it meets ' ' while string[r] is a white space character//here I'm stack increment r //what for increment w// to another string[w] = null character // well yes, it has to end somewhere ,
VIEWS ON THIS POST

116

Posted on:

Saturday 20th October 2012
View Replies!

Help figuring out problem with my first C program

I am attempting to write a program (for school) that will accept data about the length of metal rods and sort them into three categories (too long, too short, and normal), tally up the overall lengths of all three categories, and average the lengths of all three categories (and give total number of rods, total length of all rods, and total of all averages). My professor also wants the program to display an error (but continue working) if the data set continues less than or more than 20 metal rods and he wants each data entry to display on the screen immediately following its entry to ensure accuracy of entry. (phew...that's a lot of background!). Anyhow, I've attached my programmed below - I couldn't get it to copy-paste, no matter how many which ways I did the whole tags things. It just kept giving me errors and not letting my post. I think I am just cursed today in general. The problem is that I can't even get the program to run as is! I get an error telling me that the program is expecting a ; before the { of my else if statement, but I know that it incorrect (and if I put one in, it just makes even more errors!) I have spent quite a few hours trying to reformat the code thinking I must be overlooking something. I even went as far as to break all of my printf functions into separate lines (annoying, I know) just to be sure that I hadn't missed some tiny punctuation mark that was throwing off the whole darn code. Even if someone can just point me to the line or in the right direction, I would be truly grateful as I really don't know what I am missing here!
VIEWS ON THIS POST

94

Posted on:

Saturday 20th October 2012
View Replies!

Problem in my first program :/

this is my first program, a simple arithmetic, but it wont add! Code: main() { int x,y,z; scanf("%d", x); scanf("%d", y); z=x+y; printf("sum is:", z); } where did I go wrong compiler shows no error, takes input and prints only--> sum is: pretty demotivating for my first program!
VIEWS ON THIS POST

100

Posted on:

Tuesday 23rd October 2012
View Replies!

remove non-existent element in the std::set

if I provide a key of element that doesn't exist in the set, and I call set.erase(nonInSetKey), does standard guarantee that nothing "BAD" will happen, in fact, nothing at all will happen. Does it make sense to check first whether key is in the set before removing it
VIEWS ON THIS POST

116

Posted on:

Saturday 3rd November 2012
View Replies!

===Welcome to comp.lang.c++! Read this first.

Could you stop doing this
VIEWS ON THIS POST

121

Posted on:

Saturday 3rd November 2012
View Replies!

state is how you behave to events ++ the first antibodhi law

In article , galathaea wrote: [...] How about an example for the short bus -- Michael Press
VIEWS ON THIS POST

141

Posted on:

Saturday 3rd November 2012
View Replies!

how to remove warning clock skew detected

, when i use the make file given below i get the warning message.. clock skew detected your build may be incomplete.how can i remove this warning pls help me.. the make file which i use is ... obj-M:=hello.o all: make -C/lib/modules/$(shellUname-r)/build M=$(PWD)modules clean: make -C/lib/modules/$(shellUname-r)/build M=$(PWD)clean
VIEWS ON THIS POST

178

Posted on:

Saturday 3rd November 2012
View Replies!

Accidentally removed a file from project

I accidentally removed a source file from the project i was working on. Is there anyway to recover this file I am using Dev-C.
VIEWS ON THIS POST

128

Posted on:

Saturday 3rd November 2012
View Replies!

Question on auto_ptr, Which function will call first?

, I'm studying the volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include #include #include using namespace std; class TraceHeap { int i; public: static void* operator new(size_t siz) { //*****NOTE A void* p = :perator new(siz); cout
VIEWS ON THIS POST

109

Posted on:

Sunday 4th November 2012
View Replies!

Feeding string into ostringstream only uses up to the first null?

I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I tried a std:stringstream: std:stringstream oss; oss
VIEWS ON THIS POST

154

Posted on:

Sunday 4th November 2012
View Replies!

std::list remove element mid iteration

The situation is that a std::list is being iterated through. Upon certain criteria some sets become empty. I need to remove the empty sets from the list. Is it safe to iterate through a list and call list::erase( iterator ) in mid iteration
VIEWS ON THIS POST

146

Posted on:

Sunday 4th November 2012
View Replies!

===Welcome to comp.lang.c++! Read this first.

> On Wed, 31 Oct 2007 16:30:00 +0000, Shiva wrote: > Welcome to comp.lang.c++! Read this first. > ...[SNIP]... I took the responsibility of posting FAQs when you were not around and I did not fulfill it properly. I apologize for not being a responsible person here. Its nice to see that you are back Shiva :-) . BTW, Where you went I am just curious because you just disappeared. -- http://www.lispmachine.wordpress.com
VIEWS ON THIS POST

105

Posted on:

Sunday 4th November 2012
View Replies!

Re: **** Welcome to comp.lang.c++! Read this first ****

arnuld wrote: > Some netiquette topics which frequently crop up on comp.lang.c++ are also > answered in the FAQ. > > * Should I post job advertisements and/or resumes on comp.lang.c++ > http://www.parashift.com/c++-faq-lit...t.html#faq-5.9 > > * What if I really need a job; should I post my resume on comp.lang.c++ > http://www.parashift.com/c++-faq-lit....html#faq-5.10 > > * What should I do to someone who posts something off-topic > http://www.parashift.com/c++-faq-lit....html#faq-5.11 arnuld, Just a note: The FAQs have changed since the original Welcome message was written. These FAQs are now 5.10, 5.11, and 5.12, respectively. 5.9 is the "What newsgroup should I post to" FAQ. --
VIEWS ON THIS POST

201

Posted on:

Sunday 4th November 2012
View Replies!

Re: Why won't this list remove_if code compile

"Angus" wrote: > I have a class CRequest with a function: > bool IsFinished() const; > > I have a list of these CRequests - list mylist. > > I create a function object to find if a CRequest is finished: > class is_finished : public std::unary_function > { > public: > bool operator() (CRequest req) { return req.IsFinished(); } > }; > > And I use remove_if like this: > m_RequestList.remove_if(is_finished()); > > But I get a compile error: > error C2664: 'remove_if' : cannot convert parameter 1 from 'class > is_finished' to 'class std::binder2nd >' > No constructor could take the source type, or constructor overload > resolution was ambiguous > > How can I fix this Try this: class CRequest { public: bool isFinished() const; }; void fn( list lst ) { lst.remove_if( mem_fun_ref( &CRequest::isFinished ) ); }
VIEWS ON THIS POST

160

Posted on:

Sunday 4th November 2012
View Replies!

remove_copy

Dear all, it may be a friday afternoon issue, but I was looking for a remove variant which removed elements from a vector, but not throwing them away in the first place, but keeping a copy so that I still could do things with the removed elements before actually destroy them. So in the STL you have the 'remove(_if)' function which removes elements. However the 'remove_copy' does not remove elements form the original input! It has more the semantic of a 'copy_if_not' function (also implicitly pointed out by Austern, by implementing 'copy_if' in terms of 'remove_copy_if', see 12.6.4 of Generic Programming && STL). Since the STL already exist for more than a decade, it must be my own mind which interprets the function name wrong. Or it is indeed a friday afternoon issue. Of course human language is multiple interpretable and English is not my native language, so the original c+ + committee members may be 'right' after all. Above functionality is solvable btw by stable_partition as someone pointed out in CUJ. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

124

Posted on:

Sunday 4th November 2012
View Replies!

I can't read the first byte of an EXE file!!!!!

i write this code to read the first byte from my EXE and compare it with the entered EXE file the byte read from both files supposed to be the same but that's don't happened!! i tried also with different EXE's but it also failed to read the first byte which is "M" -- here is the code: FILE *a,*b; int z; char str1[2]; char str2[2]; a= fopen(argv[0],"rb"); //try to change both file with another EXE's and u will also fail b= fopen(argv[1],"rb+"); z= fread(str1,1,1,a); if (z !=1) printf("-101"); z= fread(str2,1,1,b); if (z !=1) printf("-102"); if(str1 == str2) { fwrite("a",1,1,b); // i want to change the first byte with this char printf("OK"); }else{ fwrite(str1,1,1,b); printf("NO"); } fclose(a); fclose(b);
VIEWS ON THIS POST

172

Posted on:

Sunday 4th November 2012
View Replies!

the usage of remove function in c++

can anyone tell me the usage of remove,include its' return value,prototype,a example may as well.
VIEWS ON THIS POST

121

Posted on:

Sunday 4th November 2012
View Replies!