943,653 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 388
  • C++ RSS
Mar 6th, 2009
0

Basic program ... can't figure the problem

Expand Post »
Hi all,

i made some java few time ago and i'm now studying C++
here is a simple program i've been working on to learn about it but the fact is for some days now i can't figure why it doesn't work...

in fact i just want to create an array of strings, noms[10]

my problem is that the first name will be stored all right in " noms[0] " , but then if i add a second name, it will be stored both in " noms[0] " and " noms[1] " ... and again same problem with a third name in noms[0] noms[1] and noms[2] ...

perhaps it's a real simple one but i don't really understand why data will go on " noms[0] " if i'm considering the address of " noms[1] "


thank you
vincent




#include <iostream>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <istream>


using namespace std;

int j=0;
char *noms[10];
char **ptr;

void add() {

char name[20];

cout << "Enter a name" << "\n";
cin >> name;

*ptr = name;

cout << "voici noms[" << j << "] :" << noms[j] << "\n";
cout << "voici noms[0]" << noms[0] << "\n";
return;
}




int main()
{
int x;
ptr = &noms[0];

while (j<11) {

cout << " Enter your choice :" << "\n";
cin >> x;

if ( x ==1 ) {
add();
j++;
ptr++;
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vmarc29 is offline Offline
3 posts
since Mar 2009
Mar 6th, 2009
0

Re: Basic program ... can't figure the problem

1. don't mix C and C++ this way
2. use std::string instead of char[]
3. try to avoid including .h files, the c++ header files have no trailing .h
4. to copy char[], use strcpy()
5. use code tags for better reading
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Mar 6th, 2009
0

Re: Basic program ... can't figure the problem

Please use code tags, [CODE=cplusplus][/CODE].

Firstly, is this C or C++ because it looks like a strange confusion between the two. The only header you should be using in this program is #include <iostream> not with a .h or anything else funky.

I'd suggest you go back over the very basics of C++ before attempting to use pointers etc...since you are using global variables..which is not good.

And you obviously don't understand pointers...

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Mar 6th, 2009
0

Re: Basic program ... can't figure the problem

well perhaps indeed my language is not really a C++ one; to be honest i only used what i found on forum/books/ tutorials. sorry these are not complient with your way of coding
concerning package i know but i don't really think it's the core of my problem here
concerning pointers, coming from java indeed i have some lack. now no where i found explanations about this precise point, thus i just wanted to know if someone could help me . i know it might be kinda boring for u guys to see always same mistakes form newbies, now i spent a week looking on forum and on books a solution, i found not.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vmarc29 is offline Offline
3 posts
since Mar 2009
Mar 6th, 2009
0

Re: Basic program ... can't figure the problem

Basically what this code is doing is that --

In first step you are putting some value in name and norms[0] pointing to name.

In second step you changing value in name and norms[1] now also points to name.
Since both norms[0] and norms[1] points to name new value is reflected by both of them.

Suggestion: You should have array of names so norms[0], norms[1] can point to different names.
Reputation Points: 11
Solved Threads: 3
Newbie Poster
vivekc++ is offline Offline
12 posts
since Feb 2009
Mar 7th, 2009
0

Re: Basic program ... can't figure the problem

oh ok i see ... yeah, of course .
thx a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vmarc29 is offline Offline
3 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do I correctly seek a text file using ofstream?
Next Thread in C++ Forum Timeline: Help carrying over when adding two arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC