Basic program ... can't figure the problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 3
Reputation: vmarc29 is an unknown quantity at this point 
Solved Threads: 0
vmarc29 vmarc29 is offline Offline
Newbie Poster

Basic program ... can't figure the problem

 
0
  #1
Mar 6th, 2009
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++;
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

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

 
0
  #2
Mar 6th, 2009
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
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

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

 
0
  #3
Mar 6th, 2009
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: vmarc29 is an unknown quantity at this point 
Solved Threads: 0
vmarc29 vmarc29 is offline Offline
Newbie Poster

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

 
0
  #4
Mar 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 12
Reputation: vivekc++ is an unknown quantity at this point 
Solved Threads: 3
vivekc++ vivekc++ is offline Offline
Newbie Poster

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

 
0
  #5
Mar 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: vmarc29 is an unknown quantity at this point 
Solved Threads: 0
vmarc29 vmarc29 is offline Offline
Newbie Poster

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

 
0
  #6
Mar 7th, 2009
oh ok i see ... yeah, of course .
thx a lot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC