Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 22nd, 2008, 01:10 PM
Authorized User
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default How to improve this C++ code to accept spaces in i

#include <iostream.h>
#include <conio.h>

char res;
int index=0;
char names[index][20];//names each 20 characters long
cout<<"\nEnter name "<<(index + 1)<<": ";
cout<<"\nWant to continue?";
if (res == 'y' || res == 'Y')
}while(res == 'y' || res == 'Y');
cout<<"\nprogram terminated!";

Why this program does not accept spaces?


Life is an endless journey towards perfection

Life is an endless journey towards perfection
Reply With Quote
  #2 (permalink)  
Old July 23rd, 2008, 01:42 AM
Authorized User
Join Date: Jan 2008
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts

Well cin>> just ignores spaces you need to use getline() to read a string that contains any whitespace character but there is another problem with your code : char names[index][20] = char names[0][20] here you create a string array with 0 rows and 20 columns and when you create an array like this staticaly you can not change or reallocate by index++ you will then try to reach a memory location that is outside boundaries and start to write a memory location outside of your program and this is very dangerous..since the user can add as many names as he wants cause you do not check the limits you have to allocate memory dynamically or first get the number of total names from user then check the limits.hope this helps.

Reply With Quote

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me to improve this ADO.NET code. edurazee ADO.NET 6 July 22nd, 2008 09:55 PM
Improve on this code...? niallhannon Pro VB 6 7 February 24th, 2007 01:47 PM
how to improve this code? dhaval229 SQL Language 0 February 20th, 2006 04:38 AM
how to improve this code? dhaval229 SQL Language 0 February 20th, 2006 04:36 AM
How to Improve Myself in ASP ranuji BOOK: Access 2003 VBA Programmer's Reference 4 April 22nd, 2005 08:53 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.