Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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>

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

Why this program does not accept spaces?

MAXOOD!

Life is an endless journey towards perfection
__________________
MAXOOD!

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
Default

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
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:29 PM.


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