If i use arrays. My windows will come out a warning

This is a discussion on If i use arrays. My windows will come out a warning within the C++ Programming forums, part of the General Programming Boards category; this is my code Code: #include <iostream> #include <string> #include <conio.h> using namespace std; struct booking_melakajb1 { string name; }; ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    10

    If i use arrays. My windows will come out a warning

    this is my code

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    struct booking_melakajb1
    {
    	string name;
    
    };
    
    booking_melakajb1 reading()
    {
    	booking_melakajb1 book[3];
    
    	
    	for(int i=0; i<=2; i++)
        {	
    		cout << "Please insert your name : " ;
    		cin >> book[i].name;
        }
    	cin.ignore();
    	return book[3];
    }
    
    int main()
    {
    	booking_melakajb1 show;
    	show = reading();
    	string aku;
    	string dia = "y";
    	
    	
    	cout << "Show all booking output. Type Y for yes." << endl;
    	getline (cin, aku);
    	
    	if ( aku == dia )
    	{
    		cout << show.name ;
    	}
    	system("pause");
    	return 0;
    }
    its ok when i compile it... when i run this program... then the windows will popup and says that has encaountered a problem and need to close... what shoul i do????

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > return book[3];
    This is returning an element of the array which doesn't exist.

    If you want to populate an array of books, then pass the array as an input to this function, and declare the array itself in main.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    Then... if i do that... i cant add any record for other thing...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    OK, spoon-mode on
    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    struct booking_melakajb1
    {
    	string name;
    
    };
    
    void reading(booking_melakajb1 book[3])
    {
    	for(int i=0; i<3; i++)
        {	
    		cout << "Please insert your name : " ;
    		cin >> book[i].name;
        }
    	cin.ignore();
    }
    
    int main()
    {
    	booking_melakajb1 show[3];
    	reading(show);
    	string aku;
    	string dia = "y";
    	
    	cout << "Show all booking output. Type Y for yes." << endl;
    	getline (cin, aku);
    	
    	if ( aku == dia )
    	{
    		for ( int i = 0 ; i < 3 ; i++ ) cout << show[i].name ;
    	}
    	system("pause");
    	return 0;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 05:41 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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