Problem with COUT

I'm done programming the first part for my Mad Lib program, but when I run testBed to check my program, I keep getting as if there was two COUTs together when I'm trying to ask one question at a time.

I know the problem is in my switch, but I'm not sure how to fix it.

this is the feed back I'm getting from testBed:

Started program
> Please enter the filename of the Mad Lib: madLibWeb.txt
> Web site name: Automobile Magazine.com
> \tVerb: \tPlural noun:
Exp: \tVerb:
drive
> Plural noun: cars
> \tProper noun:
Exp: \tPlural noun:
motorcycles
> \tAdjective:
Exp: \tProper noun:
New York
> \tNoun: \tNoun:
Exp: \tAdjective:
red
> \tBoolean operator:
Exp: \tNoun:
Porsche
> Noun: BMW
> \tFavorite website:
Exp: \tBoolean operator:
xor
> \tAnother website:
Exp: \tNoun:



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
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
using namespace std;

const int MAX_WORDS = 256;
const int MAX_WORD_SIZE = 32;

void getFileName(char filename[])
{
   cout << "Please enter the filename of the Mad Lib: ";
   cin >> filename;
}

void prompt(char array[])
{
    cout << "\t" << (char)toupper(array[1]);

   for (int i = 2; array[i] != '>'; i++)
   {
      if (array[i] == '_')
         cout << " ";
      else
      {
         cout << (char)tolower(array[i]);
      }
   }

   cout << ": ";
   cin >> array;
}


int readFile(char filename[], char array[][MAX_WORD_SIZE])
{
   ifstream fin(filename);

   if (fin.fail())
      return -1;

   int i = 0;
   while (fin >> array[i])
   {
      if (array[i][0] == '<')
      {
         switch (array[i][1])
         {
            case '#':
            case '{':
            case '}':
            case '[':
            case ']':
               break;
            default:
               prompt(array[i]); 
         }
      }
   }
   fin.close();
   return 0;
}


int main ()
{
   char fileName[256]; 
   getFileName(fileName);
   char array[MAX_WORDS][MAX_WORD_SIZE];
   readFile(fileName, array);
   cout << "Thank you for playing.\n";
   return 0;
}

Last edited on
I've added a cin.ignore before prompt(array[i]); and it partially solved the problem. Any ideas?
Last edited on
Reading into a char array with >> will only read until it hits a whitespace. Any characters after that are left in the buffer, which are left for the next prompt to read. If you want to read whole lines into strings, consider using the std::string type and the std::getline() function.
The problem is that I'm not allowed to use string for this project.
thanks for the help. I was able to solve it writing

1
2
3
cin.clear();
cin.ignore(80, '\n');
prompt(array[i]);


Topic archived. No new replies allowed.