Problem with array in displaying a string

This is a discussion on Problem with array in displaying a string within the C++ Programming forums, part of the General Programming Boards category; This is my code in writing a file. The problem I have is when I input a data in "Enter ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    14

    Unhappy Problem with array in displaying a string

    This is my code in writing a file. The problem I have is when I input a data in "Enter Name: ", lets say I input Sarah Jessica it writes weird output in my file. This is th weird output:

    Name: Sarah
    age:-858993460
    address:Jessica

    My question is:
    1. Why is it that my program can't accept input with space?
    eg. Sarah Jessica<-it ignores string sarah
    2. Whats that wierd -858993460
    3. Why is it that string sarah jumps to Address?
    I need some experts thoughts. Thanks a lot in advance
    Code:
    #include"include.h"
    main()
    {
    	ofstream out_stream;
        
    	char out_file_name[20];
    	char name[20];
    	char adr[20];
    	int age;
    	printf("\nTHIS IS A MINI DATABASE PROGRAM");
    	printf("\n\nEnter Name:");
    	scanf("%s",&name);
    	printf("You entered %s as name", name);
        
    	printf("\nEnter age:");
                    scanf("%i",&age);
    	printf("You entered %i as age", age);
    
    	printf("\nEnter address:");
    	scanf("%s", &adr);
    	printf("\nYou entered %s as address:", adr);
       
       out_stream.open(out_file_name);
       
       if(out_stream.fail())
       {
    	   printf("Output File opening failed.\n");
    		   exit(1);
       }
    
       out_stream<<"Name:"<<name<<endl<<"age:"
    	         <<age<<endl
    			 <<"address:"<<adr<<endl;
       
       out_stream.close();
       return 0;
    	
    }
    Flamers are worst than Newbies

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Mixing C and C++ code is not a good idea.
    Read this:
    <iostream> and <stdio.h> can be used in the same program. The easiest way to mix them is to make sure that no single file is manipulated using both <iostream> routines and <stdio.h> routines.

    If any given file needs to be manipulated by both <iostream> routines and <stdio.h> routines, special considerations must be taken into account. Make sure that ios::sync_stdio(false) has not been called. If it has then call ios::sync_with_stdio() as shown.
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
      ios::sync_with_stdio();                            <-- 1
      // ...
    }
    (1) No I/O should occur before this line



    Note that this synchronization can degrade I/O performance, so it should be used only if <iostream> routines and <stdio.h> routines are manipulating the same file. For example, synchronization is needed if the program reads from both cin and stdin, or if it writes to both cout and stdout. But if <iostream> routines and <stdio.h> routines are not manipulating the same file, synchronization is unnecessary and should not be used.


    Dont use:
    Code:
    scanf("%s", &adr);
    Use:
    Code:
    scanf("%s", adr);
    instead (ofcourse first decide what to use cstdio or iostream!).
    adr is already pointer so you don't need to use addressof operator &

    I would advise you to use cout and cin intead of printf and scanf.
    What if user decide to enter name like this: John Smith. your scanf will pick up only John.
    What is the name of your file anyway:
    Code:
     out_stream.open(out_file_name);
    What is the value of out_file_name.

    And finally you should read the FAQ the difference between \n and endl
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Cheers!
    Last edited by Micko; 10-17-2004 at 10:31 AM.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    14

    Talking

    Hi Micko I got it working already but thanks to the advice. I used cin.getline() instead of scanf() and it works perfectly fine

    Here's my final code tell me what you think.

    Code:
    #include"include.h"
    
    main()
    {
    	ofstream out_stream;
       
    	char out_file_name[20]="text.txt";
    	char name[20];
    	char adr[20];
    	
    	int age;
    	
    	cout<<"\nTHIS IS A MINI DATABASE PROGRAM";
    	cout<<"\n\nEnter Name:";
        cin.getline(name, 20);
    	
        printf("Enter age: ");
    	scanf("%d", &age);
    
    	cout<<"\nEnter address:";
    	cin.getline(adr, 20);
    
       out_stream.open(out_file_name);
       if(out_stream.fail())
       {
    	   printf("Output File opening failed.\n");
    		   exit(1);
       }
    
       out_stream<<"Name:"<<name<<endl<<"age:"
    	     <<age<<endl
    	     <<"address:"<<adr<<endl;
       
       out_stream.close();
       
       
       return 0;
    	
    }
    Flamers are worst than Newbies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. problem with string and array
    By Methje in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2006, 12:15 PM
  4. string array input problem
    By DoItAllMom115 in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2003, 09:38 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21