0

i am new into coding, and im trying to make a code that saves names, but when i run it, it only saves the first letter of the input, idk what i did wrong the languaje is cpp, and this is the code

#include <iostream>
using namespace std;

int main() {
    int cv;
    cout<<"Cuantas personas van a participar? "<< endl;
    cin>> cv ;
    char nombres[cv]{};
    for(int x = 1; x<=cv; x++){
        cin>>nombres[x];
        cout<<nombres[x]<< endl;
    }
    return 0;
}
  • 3
    `char nombres[cv]` is wrong. See [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/q/1887097/10147399). If you are new to C++, stop learning from where you learned to code like that. Instead see [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/10147399). – Aykhan Hagverdili Jul 31 '21 at 02:37
  • 3
    Even if you fix to avoid using VLA, indexing of array elements in C++ is zero based. Your loop treats indexing as 1-based. – Peter Jul 31 '21 at 02:47
  • In addition to @Peter 's comment, maybe use a 2D character array for storing names or use the `string` class. – DeBARtha Jul 31 '21 at 04:03

4 Answers4

1

Just replace

char nombres[cv]{};

with

string nombres[cv]{};

Only first letter was getting stored because char stores only one character. String is used to store an array of characters (basically multiple characters)

0

char data type only stores a single character. To store a sequence of characters, use string instead. Also, the index of an array starts from 0. Always start the for loop from 0.

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int cv;
    cout<<"Cuantas personas van a participar? "<<endl;
    cin >> cv;
    string nombres[cv]{};
    for(int x = 0;x < cv;x++)
    {
        cin >> nombres[x];
        cout << nombres[x] << endl;
    }
    return 0;
}
0
#include <iostream>
using namespace std;
int main(){
int cv;
cout<<"Cuantas personas van a participar? "<< endl;
cin>> cv ;
char nombres[cv];
for(int x = 0; x<cv; x++){
    cin>>nombres;
    cout<<nombres<< endl;
}
return 0;}

This is a simple and easy approach for the same task. By using nombres instead of nombres[], we are inputting string as a whole without entering characters one by one.

In your given code, you are using 'int cv' as the string length as well as the termination condition of the for loop. Instead you can take two different integers for the respective tasks. Always try to start your 'for loop' with x=0 . By taking x=1 as initialization, nombres[0] is left blank and therefore can create errors.

-1
#include <iostream>
using namespace std;

int main() {
    int cv;
    cout << "Cuantas personas van a participar? "<< endl;
    cin >> cv ;
    string nombres[cv]{};
    for(int x = 0; x< cv; x++){
        cin >> nombres[x];
    }
    for(int y = 0; y< cv; y++){
        cout << nombres[y] << endl;
    }
    return 0;
}

Hopefully this, helps, not sure if I correctly understood your question.

  • Maybe add some insight into your solution. Why wasn't OP's code working? What are you doing differently that solves the problem? – stateMachine Jul 31 '21 at 02:36