Thread: My thread titles are crap, and I don't give a damn - just read the post to find out

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    My thread titles are crap, and I don't give a damn - just read the post to find out

    Why does this program prints many times the red point?
    thanks in advance...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct myInfo{
    	char my_name[81];
    	char my_phone[81];
    	char my_street[81];
    	char my_email[81];
    };
    
    struct Info{
    	char name[81];
    	char phone[81];
    	char street[81];
    	char email[81];
    };
    
    // It prints name, phone, address, and e-mail.
    void print(struct Info info){
    	printf("Diagnostic.\n\n");
    }
    
    int main(void){
    	int choice;
    	char buf[6];
    	FILE *infile;
    	struct Info info;
    	struct myInfo myinfo;
    
    		print(info);
    		// Opening the file.
    		infile = fopen("file.txt","r+");
    		
    		// Check if the file exist.
    		if(infile==NULL){
    			printf("Error :(\n");
    			exit(1);
    		}
    		
    		// Reading the file. Read: name, phone, address and e-mail. It read and prints, reads prints, reads prints...
    		while(fscanf(infile,"%s %s %s %s", info.name, info.phone, info.street, info.email) != EOF){
    			print(info);	
    		}
    
    		printf("If you want to put a new contact please type: 1\n");
    		printf("To exit please type: 0\n");
    		
    		scanf("%d", &choice);
    
    		if(choice==1){ //Giving info for a new contact.
    			printf("Please type your name:");
    			scanf("%s", myinfo.my_name);
    
    			printf("Please type your phone:");
    			scanf("%s", myinfo.my_phone);
    			
    			printf("Please type your street:");
    			scanf("%s", myinfo.my_street);
    
    			printf("Please type your e-mail:");
    			scanf("%s", myinfo.my_email);
    
    			//These are the info the user typed.
    			printf("Your name is: %s\n", myinfo.my_name);
    			printf("Your phone is: %s\n", myinfo.my_phone);
    			printf("Your name is: %s\n", myinfo.my_street);
    			printf("Your name is: %s\n", myinfo.my_email);
    
    			//Puting them into the file, using fprintf.
    			fprintf(infile,"%s %s %s %s", myinfo.my_name, myinfo.my_phone, myinfo.my_street, myinfo.my_email);
    		}else if(choice==0){
    			printf("Are you sure yes/no?\n");
    			scanf("%s", buf);
    			if(buf=="yes"){
    				exit(1);
    			}else{
    				printf("I was hoping to come back!\n");
    			}
    			
    		}
    
    		fclose(infile);
    
    	return 0;
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Because of this:
    Code:
    		// Reading the file. Read: name, phone, address and e-mail. It read and prints, reads prints, reads prints...
    		while(fscanf(infile,"%s %s %s %s", info.name, info.phone, info.street, info.email) != EOF){
    			print(info);	
    		}
    Research what scanf is doing here. Maybe also print the DATA in your print function.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    After 49 freaking posts you still don't realize how inappropriate "Help!!!!!!!!!!" is as a topic title? Ridiculous.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by rags_to_riches View Post
    After 49 freaking posts you still don't realize how inappropriate "Help!!!!!!!!!!" is as a topic title? Ridiculous.
    This was a wrong answer my friend...ridiculous are now your words....

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure - let us know how that works out for you.
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    ::hijack::
    Quote Originally Posted by Salem
    Salem
    and the hat of Destiny
    Dude! Why did you take Destiny's hat? Does she know? Is this a friend of yours?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    81
    why don' t you sit down and write a program? it is better than talking....also, it is very ridiculous when some persons comment on other useless aswers....he could have writen this in a better way...!!!!!!!!!! think a little.....

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    nullifyed, read the forum guidelines:
    2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nullifyed View Post
    why don' t you sit down and write a program? it is better than talking....also, it is very ridiculous when some persons comment on other useless aswers....he could have writen this in a better way...!!!!!!!!!! think a little.....
    The purpose is to help you help yourself, make things easier for everyone, etc. Salem gives more real advice around here than almost anyone else some days, recognize that, and TRIM YOUR EGO. It's not a personal attack.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the program, you have two structs defined: Info and myInfo. What is obvious, is that they are the same definition, only having a different name.

    Clearly, what you wanted was another *instance* of the Info struct.

    You can initialize another instance of the struct, in any function you want, because the Info definition is above main(), and outside any function. It's *global* in scope.

    So you can delete the myInfo struct definition, and just create another instance of an Info struct, and you'll be working better, right away.

    Your other question was already answered, I believe. If not please re-state it.

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You put the print(info) in a while loop. Since it's still not equal to EOF, it will execute the print function again.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Showtime....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    81
    I was talking for this::: "After 49 freaking posts you still don't realize how inappropriate "Help!!!!!!!!!!" is as a topic title? Ridiculous."...i don not want to have any problems with Salem(and sorry for previous message)...Ok. i made a mistake...i wrote a wrong title...so what? i' ll be happy to change it if SOMEBODY posted to me and say: "Hey i believe you tite is a bit wrong...you should change it ...". Am i wrong????

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are obviously more concerned about breaking the forum rules than helping yourself. I think this thread should be closed. But what good would that do? You would just repost the same questions 20 times ..........ing about how the rules are not tailored to your needs.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you're wrong - now go away and read some FAQ's on how to post with a decent title.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed