| | |
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:
Solved Threads: 0
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++;
}
}
}
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++;
}
}
}
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
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
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
Please use code tags - Please mark solved threads as solved
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
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
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
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Feb 2009
Posts: 12
Reputation:
Solved Threads: 3
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.
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.
![]() |
Similar Threads
- Beginner's Problem (Java)
- Dynamic Array - basic program, please help (C++)
- Dead end with basic code (C)
- Basic Programming help (C++)
- Need Help with a part of my encryption program PLEASE! (C#)
- Problem with Turbo C...or so I think!! (C++)
- help writing a grep program (C)
- cannot convert from 'double' to 'float [6][3]' (C)
Other Threads in the C++ Forum
- Previous Thread: How do I correctly seek a text file using ofstream?
- Next Thread: Help carrying over when adding two arrays
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





