dimanche 26 juin 2016

Faulty Conditionals using '{' and '[' in c++ [on hold]


For an assignment in my introduction to Software Development class, we have to create a program to accept a filename from the user and read the Mad Lib contained in that file. The prompts look like < adjective > (No spaces). Everything seems to be working but I am getting output spaces after the opening quotes and single quotes. (Ex: words words words, " words response words." (which looked like words words words , <{> words < prompt > words . <}> in the input file.) The prompt to enter a quotation mark looks like <{> and single quote <[>.

151    int j = 0;
152    //Take the array of words and display them appropriately.
153    for (int i = 0; i < total; i++)
154    {
155       //Check to see if this word is a prompt.
156       if (words[i][0] != '<')
157          //If it is not, display the word.
158          cout << words[i];
159       //If it is then see what is supposed to be done.
160       if (words[i+1][0] == '<')
161       {
162          //New line
163          if (words[i+1][1] == '#')
164             cout << "n";
165          //Opening quotation mark
166          else if (words[i+1][1] == '{')
167             cout << " "";
168          //Closing quotation mark
169          else if (words[i+1][1] == '}')
170             cout << """;
171          //Opening single quotation mark
172          else if (words[i+1][1] == '[')
173             cout << " '";
174          //Closing single quotation mark
175          else if (words[i+1][1] == ']')
176             cout << "'";
177          //Next response in the list
178          else if (islower(words[i+1][1]))
179          {
180             cout << " " <<  responses[j];
181             j++;
182          }
183       }
184       //If the next word is not a prompt or punctuation and this word is not
185       //a new line or open quotation display a space.
186          if (isalnum(words[i+1][0]) && words[i][1] != '#' && words[i][1] != '{' && words[i][1] != '[')
187             cout << " ";

Aucun commentaire:

Enregistrer un commentaire