Hello

I am working on this program and I cannot seem to get it working. I am trying to get an input of a line using cin.getline() but i am unable to get that line and assign it into an array of characters. Everything compiles correctly but the alphabet[50]'s output is blank when executed.

For example:

int main() {
   char line[50];
   num = 0;
   char alphabet[27];

 cin.getline(line,27);
 num = 0;
 while (line[num++] != 0);
 num -=2;
 for (int i = 0; i < num; i++) {
           line[i] = alphabet[i];
 }
}

Can someone explain to me what I am doing wrong?
Thanks

Recommended Answers

All 5 Replies

>>Can someone explain to me what I am doing wrong
EVERYTHING

This is what you want :

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

int main(){
 string line;
 getline(cin,line); //read a line
 /* do stuff with the line */
}
commented: OP asked for what he's doing wrong, not just code that fixes the problem. -3

>>Can someone explain to me what I am doing wrong
EVERYTHING

This is what you want :

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

int main(){
 string line;
 getline(cin,line); //read a line
 /* do stuff with the line */
}

I did not explain clearly. I am trying to maintain using the

cin.getline(line,50);

as the program will read in many lines. The first line it reads will be the alphabet "abcdefghijklmnopqrstuvwxyz" I need to store that first line into the alphabet array. As I will be using it later in the program.

I think you have line 11 backwards, it should be

alphabet[i] = line[i];

also, I'd probably just change the program so that it looks like:

int main()
{
    char alphabet[27];
    cin.getline(alphabet,27);

    char line[50];
    cin.getline(line, 50);
    /* Do things with line now */
}

This has the benefit that you'll never write over the end of alphabet , which is possible in the original code, if num turns out to be bigger than 27.

Do you have a good reason to use a char array, rather than a std::string ? If not, it's probably a lot easier to use a std::string , you can always use std::string::c_str() to convert it to a char array if you need it.

I think you have line 11 backwards, it should be

alphabet[i] = line[i];

also, I'd probably just change the program so that it looks like:

int main()
{
    char alphabet[27];
    cin.getline(alphabet,27);

    char line[50];
    cin.getline(line, 50);
    /* Do things with line now */
}

This has the benefit that you'll never write over the end of alphabet , which is possible in the original code, if num turns out to be bigger than 27.

Do you have a good reason to use a char array, rather than a std::string ? If not, it's probably a lot easier to use a std::string , you can always use std::string::c_str() to convert it to a char array if you need it.

Thank you, ravenous! The program works correctly now. I cannot use std::string or std::string::c_str() as I have yet to learn it in my class.

I cannot use std::string or std::string::c_str() as I have yet to learn it in my class.

Ah, then you're in for a treat :o)

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.