skipping the '\n' in the array when reading an getline input

in my program i have a txt file that has the next line character at the beginning of each line and my program will not run correctly with that. (it's much better of i show a screenshot, i'm not good at explaining this.)
http://imgur.com/DRUgOwR

as you can see, Mary Peterson does not have next line char in front of it. so if i typed in Mary Peterson as the searchName, my program will be able to find the name in the array. however, for the other names they have the next line char in front of it. so if i were to type in the second name in the array , "Jake Andersen" ,the program will run and tell me the name is not found. how can i overcome this problem?

here is the code i have for this section of the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 cout << "Enter name for search: ";
				cin.ignore();
				getline(cin, searchName, '\n');
				

				for (int i = 0; i < 5; i++)
				{

					searchName == student_name[i];

					if (searchName == student_name[i])
					{
						found = true;
						break;
					}
				}

				if (found == true)
				{
					cout << "found" << endl << endl; 
				}
				else
					cout << "That name could not be found" << endl;
Last edited on

I have to point out that I'm also a beginner,but this worked perfectly when run it on my compiler.

const int student=5;

cout << "Enter name for search: ";
cin.ignore();
string searchName;
string student_name[student];
bool found;
getline(cin, searchName, '\n');


for (int i = 0; i < 5; i++)
{

if (searchName == student_name[i])
{
found = true;
break;
}
}

if (found == true)
{
cout << "found" << endl << endl;
}
else
cout << "That name could not be found" << endl;
ah this is my fault, i meant was that the following names like jake andersen should show up as found, but because they have that next line character in front of it, the program runs it and shows up as not found.

for the searchName, you should just type in "Jake Andersen" and the program should run and say the name has been found. but in the array, it is stored as "/nJake Andersen" and thus program shows not found when user types in "Jake Andersen".
Last edited on
I'll try to point out whats wrong instead of just rewriting your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <string>
#include <iostream>
// you are using things in this program from the std namespace
// a namespace is sort of like a container holding a bunch of
// stuff you can use in your program. The namespace that c++ uses
// standard is called "std"
// so you need to have this line proceding any use of stuff contained in std
// alternatively, you can also prefix anything from std with "std::" to indicate
// that you're using something from that namespace.
// like std::cout or std::cin.ignore() or std::string

using namespace std;

int main()
{

  cout << "Enter name for search: ";
  cin.ignore();
  // the variable "searchName" has not been declared
  //  the compiler has no idea what it means.
  // prior to the line below, you need to declared
  //  searchName. Its expected to be type:  std::string
  //  so somewhere before you call "getline" you need to have

  string searchName;
  // now searchName has a meaning and a location in memory
  getline(cin, searchName, '\n');

  // student_name is not declared and has no meaning
  // without it being declared (like before) theres no way
  // for the compiler to make understand it, and so it fails to compile
  // you need to declare this array outside of the for() loop as well for
  // this particular usage.
  // since the loop is making 5 total iterations and checking the array
  // 5 times, you need at least an array of 5 otherwise the program will crash
  // when it runs. Worse yet, it might NOT crash, and then bad thing can happen
  // when you start doing things outside of the expected memory locations.
  string student_name[5];

  for (int i = 0; i < 5; i++)
  {
    // the line below is only an expression.
    // the "==" operator compares the two things it is between
    // but without using the result of the comparison, it is useless
    // the if() will use the result of the comparison by running the code
    // which comes after it only if the result of the "==" is true;
    // so remove the line below
    searchName == student_name[ i ];

  
    if (searchName == student_name[ i ])
    {
      found = true;
      break;
    }
  }

  if (found == true)
  {
    cout << "found" << endl << endl; 
  }
  else
    cout << "That name could not be found" << endl;
}
i should mention again that this is just a snippet of my code i have already done everything else leading up to this problem in my code. if you would like me to show you my entire code then i will if it helps fix my problem. but there are multiple options my program is supposed to do, and i just have a problem in this portion of my program.

but going back to the problem again, it is more clearly stated in my second post when i was replying to Akbasha. in the link i posted above, the user is able to type in "Mary Peterson" and the program will find and display that it has found that name within the array. however, if the user is to type in the second name in the array, "Jake Andersen" the program will display that it is not found despite the name actually being in there. However, inside the array it is stored as "\nJake Andersen". i'm not sure how to get past this problem. if it helps, here is the txt. file

Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
Last edited on
getline() ceases reading at the newline, so it's correctly receiving nothing if the newline character \n is the first thing it reads in. You also don't need to provide '\n' as a delimiting character either, since it implicitly does that. Try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "Enter the name of a student, then press enter. You will be\n"
   << "entering the names of three students:\n"
string string_array[3];
for(unsigned i = 0; i != 3; ++i) getline(cin, string_array[i]);

cout << "Enter a search name: ";
string search;
getline(cin, search);

bool found = false;
for(unsigned i = 0; i != 3; ++i){
     if (search == string_array[i]){
          found = true;
          break;
     }
}

if(found) cout << "FOUND!\n";
else cout << "Not found :(\n";

Last edited on
How do you load the file ?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
    const int NO_RECORDS = 20;
    
    std::string name[NO_RECORDS];
    int grade[NO_RECORDS][5] = {0};
    char separator;
    std::string ending; // this gets rid of ,\n and any other junk at end of line
    
    std::ifstream myfile ("gradesComma.txt");
    
    int i = 0;
    if (myfile.is_open())
    {
        while (
               getline(myfile, name[i], ',')
               
               &&
               myfile
               >> grade[i][0] >> separator >> grade[i][1] >> separator
               >> grade[i][2] >> separator >> grade[i][3] >> separator
               >> grade[i][4]
               
               &&
               getline(myfile, ending)
               )
            i++;
        
        myfile.close();
        
        for (int j = 0; j < i; j++)
        {
            std::cout << j << ". " << name[j] << ' ';
            for(int k = 0; k < 5; k++)
                std::cout << grade[j][k] << ' ';
            std::cout << '\n'; // <-- this wasn't needed in the 'corrupt' input
        }
        
        for(int j = 0; j < i; j++)
        {
            if (name[j] == "Jim Blair" )
                std::cout << "Bingo!\n";
            else
                std::cout << "Not this one.\n";
        }
            
    }
    else
        std::cout << "Unable to open file";
    
    return 0;
}


Input
Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,



Output
0. Mary Peterson 95 93 77 94 77 
1. Jake Andersen 90 90 95 93 49 
2. Susan Cooper 79 94 44 90 73 
3. Mike Smith 95 93 30 79 97 
4. Jim Blair 53 45 97 39 59 
5. Clark Lee 70 95 45 39 77 
6. Kennedy Davis 77 34 55 74 93 
7. Kim Bronson 93 94 99 77 97 
8. Sunny Hill 79 95 59 93 95 
9. Sam Benson 95 75 49 75 73 
Not this one.
Not this one.
Not this one.
Not this one.
Bingo!
Not this one.
Not this one.
Not this one.
Not this one.
Not this one.
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.