As this is my first post on this forum I want to say "Hello!" to everybody and I hope that we'll help each other as much as we can and we'll be very good friends. :)

So, this is my problem ( a rather simple one but I don't know how to resolve it ) : I found a exercise that wants me to have a array with some student's names and another array with their grades ( numerical grades ) at informatics. :eek:

Example: X=('John','Michelle','Jack','Robert'); Y=(10,5,6,7);

My question is: how do I put those names in the array so that a name ( let's say John ) should occupy a single position? For example: X[1]= 'John'; X[2]='Michelle' , etc. I have to read them from the keyboard , of course. :)

Thank you very much for your time and eventual help! ;)

Hi djextazy,
Well come to DaniWeb ,
if u want to have full name at one location of the array
u have to declare array as a two dimensional array,
i-e its declaration would be like this
char [][];
or u can also use array of pointers , but if u r new to C++ u should use
two dimension array
I hope it resolved ur issue but still if their is some problem feel free to ask again.
Fahad

Hi,
The declaration should be like this
char X[][];

Fahad

Hi,
The declaration should be like this
char X[][];

Fahad

Do you mean something like
a[1][1] = "Victor";
a[2][2] = "Jose"; ???

I mean:
char a[30][30],i,n;
cout<<"n= "; cin>>n;
for(i=1;i<=n;i++) { cout<<"Give "<<i<<"-st name: "; cin>>a; }

Correct me please if I am mistakening. Thank you for your help!

Hi,
Yes u r right ,
try this............and if still getting some problems discuss it again
Fahad

An array of size N will be indexed from 0 to N-1. The standard idiomatic for loop is then like this:

for ( int i = 0; i < N; ++i )

It works , altought the exercise says that I should use two arrays , one for the names and one for the grades. Oh well , it's important that it works ... :)

Thank you very much!

An array of size N will be indexed from 0 to N-1. The standard idiomatic for loop is then like this:

for ( int i = 0; i < N; ++i )

It works fine as for(i=1;i<=n;i++) . Trust me. :lol:

>It works fine as for(i=1;i<=n;i++) .
Yes it does. But then again, so does this:

i = 9;
loopie:
  // Do stuff
  if ( --i > 9 - n )
    goto loopie;

Most programmers prefer to use the idiomatic approach because it's easier to use, easier to get used to, and common enough that you don't have to get used to something else everytime you read a different person's program.

It works fine as for(i=1;i<=n;i++) . Trust me. :lol:

Do you see the bug here? ;)

#include <stdio.h>
 
 #define N 10
 
 int main()
 {
    int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
    for ( i = 1; i <= N; ++i )
    {
 	  printf("array[%d] = %d\n", i, array[i]);
    }
    return 0;
 }

Do you see the bug here? ;)

#include <stdio.h>
 
 #define N 10
 
 int main()
 {
    int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
    for ( i = 1; i <= N; ++i )
    {
 	  printf("array[%d] = %d\n", i, array[i]);
    }
    return 0;
 }

Hehe. :) It should be like this:

for(i=1;i<=n;i++)

NOT

for(i=1;i<=n;++i)

The second one is wrong because i increases with 1 BEFORE entering the for .. so i practically starts with the value 2. While in my example , i starts from 1 and ends at 10 , meaning exactly what it has to do. ;) :eek:

Hehe. :) It should be like this:

for(i=1;i<=n;i++)

NOT

for(i=1;i<=n;++i)

The second one is wrong because i increases with 1 BEFORE entering the for .. so i practically starts with the value 2. While in my example , i starts from 1 and ends at 10 , meaning exactly what it has to do. ;) :eek:

No, the second one is not wrong, and your explanation is incorrect. I take it you didn't even try it.:rolleyes:

And I take it that you still don't see the bug. This is exactly why you shouldn't be doing this.

No, the second one is not wrong, and your explanation is incorrect. I take it you didn't even try it.:rolleyes:

And I take it that you still don't see the bug. This is exactly why you shouldn't be doing this.

Indeed , I haven't tested them until now. It seems like both versions give the same result ...

My guess is: i should start from 0? :p

Indeed , I haven't tested them until now. It seems like both versions give the same result ...

My guess is: i should start from 0? :p

Well, yes, but the bug is that you should not attempt to access array[10] because it is beyond the array bounds.

>My guess is: i should start from 0?
If you have to guess, don't be so quick to answer questions because you'll probably be wrong. Even if you bother to test your assumptions and they pan out, you could still be wrong on something more subtle.

hi
look if u want array and every case on array it's name look:
#include<stdio.h>
int main()
{char *a[5]={"sami","rami","wael","dani","nami"};
for (i=0;i<5;i++)
printf("%s",a);
return 0;
}

--------------------------------------------------------------------------------
hi
look if u want array and every case on array it's name look:
#include<stdio.h>
int main()
{char *a[5]={"sami","rami","wael","dani","nami"};
for (i=0;i<5;i++)
printf("%s",a);
return 0;
}

--------------------------------------------------------------------------------
hi
look if u want array and every case on array it's name look:
#include<stdio.h>
int main()
{char *a[5]={"sami","rami","wael","dani","nami"};
for (i=0;i<5;i++)
printf("%s",a);
return 0;
}

I want it with #include <iostream> and all the rest ( cout & cin) . This one doesn't work:

#include <iostream>
#include <conio.h>
using namespace std;
main()
{ char *a[5]; int i;
for(i=0;i<5;i++) { cout<<"Dati numele #"<<i+1<<": "; cin>>a[i]; }
for(i=0;i<5;i++) cout<<a[i]<<endl;
getch();
}

It freezes after I input 2 names. :rolleyes: What should I do?

char *a[5];

An array of uninitialized pointers. These are not automagically strings, you need to have them actually point to some memory you can write to before you try to write to it. Or actually declare an array for strings.

char a[5][80];
char *a[5];

An array of uninitialized pointers. These are not automagically strings, you need to have them actually point to some memory you can write to before you try to write to it. Or actually declare an array for strings.

char a[5][80];

I actually worked out the exercise with a double array , as you said ( a[5][80 ). But I was just churious about mohammad's version . :rolleyes:

In mohammad's version, the pointers are initialized (and the strings to which they point are not writable).

Hi,
This may resolve the problem but this is a very basic type. can anyone give a good example to store the names using pointers?

Do you see the bug here? ;)

#include <stdio.h>
 
 #define N 10
 
 int main()
 {
    int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
    for ( i = 1; i <= N; ++i )
    {
 	  printf("array[%d] = %d\n", i, array[i]);
    }
    return 0;
 }

You need to count numbers in array:

x = sizeof(array)/sizeof(array[0]);

Oh well , it's important that it works ... :)

Normally your would always use 0 for the first in the array, and as you can see in the example that @Dave made, it's easy to get confused and get unexpected output, so you should just get into the habit of always using 0.

About your comment above, it may be working, but you also what it to be good, efficient and easy to look over if you ever want to go back to it. "Just working" is not the best approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.