Question

How can i specify the values to be outputted in an array? heres my code

#include<iostream.h>
#include<conio.h>
#include <string.h>
int main(){
    clrscr();
    char name[5][80] ={ "Lester", "Charmander", "Squirtle", "Bulbasor","Pikachu"};
    int a[5];
    int i;
    int n;
    for(n=0; n<=10; n++){
    cout <<"Enter your student number: ";
    cin>>a[i];
    if(a[i]==1) {cout<<"Lester\n"; }
    if(a[i]==2) {cout<<"Charmander\n"; }
    if(a[i]==3) {cout<<"Squirtle\n"; }
    if(a[i]==4) {cout<<"Bulbasor\n";}
    if(a[i]==5) {cout<<"Pikachu\n";  break;}
            }
    clrscr();
    int k;
    for(k=0; k<6; k++){
    cout << name[k]<<"\n";
    }
    return 0;
}

heres the output of the code above

Enter your student number: 1
Lester
Enter your student number:2
Charmander
Enter your student number:5
Pikachu

Lester
Charmander
Squirtle
Bulbasor
Pikachu

It outputted all the values of the array. But i want an output that should look like this

  Enter your student number: 1
    Lester
    Enter your student number:2
    Charmander
    Enter your student number:5
    Pikachu

    Lester
    Charmander
    Pikachu
Was it helpful?

Solution

Disclaimer: I have not worked with Turbo C++ before, but from what I gather it is essentially C with a subset of C++ features.

I'm not sure if you got @rockStar's version to work so I will post a slightly cleaner and working version on your main.

int main(){
    clrscr();
    char name[5][80] ={ "Lester", "Charmander", "Squirtle", "Bulbasaur","Pikachu"};
    int numbers[10]; //'temporary array' to store the list of numbers
    int n;
    int num = 0;
    for(n=0; n<=10 && num != 5; n++){
      cout <<"Enter your student number: ";
      cin >> num;
      if(num > 0 && num < 6){
        cout << name[num-1] << '\n';
        numbers[n] = num;
      }
    }
    clrscr();
    int i;
    for(i=0; i<n; i++){ //there were n entries
      cout << name[numbers[i] -1]<<"\n";
    }
    return 0;
}

It produces the output you requested (minus the indentation of everything past the first line).

OTHER TIPS

I have added my code and include a comment, hope this help, and if theres some error plz provide as I don't have a compiler with me.

#include<iostream.h>
#include<conio.h>
#include <string.h>
int main(){
    clrscr();
    char name[5][80] ={ "Lester", "Charmander", "Squirtle", "Bulbasor","Pikachu"};
    int a[5];
    //temporary array
    int temp_array[10];
    int i;
    int n;
    for(n=0; n<=10; n++){
    cout <<"Enter your student number: ";
    cin>>a[i];
    if(a[i]==1) {cout<<"Lester\n"; temp_array[a[i]] = a[i]; }
    if(a[i]==2) {cout<<"Charmander\n"; temp_array[a[i]] = a[i];}
    if(a[i]==3) {cout<<"Squirtle\n"; temp_array[a[i]] = a[i];}
    if(a[i]==4) {cout<<"Bulbasor\n"; temp_array[a[i]] = a[i];}
    if(a[i]==5) {cout<<"Pikachu\n";  temp_array[a[i]] = a[i]; break;}
            }
    clrscr();
    int k;
    for(k=0; k<6; k++){
    if(temp_array[k] != '') //checks if the value of that array is not empty
      cout << temp_array[k]<<"\n";
    }
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top