Creating a newline character every 7 numbers

I'm doing a programming assignment where the file reads input from a file(the file is all letters) and converts it to a phone number. I have the program working, but i can't figure out how to make it output an endline character every 7 to the file. Please help!

code:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

int main() {
char oldNumber;

fstream infile;
infile.open("oldPhone.txt");

ofstream outfile;
outfile.open("newPhone.txt");

if (!infile) {
cerr << "Could not open file for input ---- Terminating " << endl;
exit(1106);
}
while (infile.get(oldNumber)) {
char toupper(oldNumber);

switch (oldNumber) {

case 'A':
case 'B':
case 'C': 2;
outfile << 2;
break;
case 'D':
case 'E':
case 'F': 3;
outfile << 3;
break;
case 'G':
case 'H':
case 'I': 4;
outfile << 4;
break;
case 'J':
case 'K':
case 'L': 5;
outfile << 5;
break;
case 'M':
case 'N':
case 'O': 6;
outfile << 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S': 7;
outfile << 7;
break;
case 'T':
case 'U':
case 'V': 8;
outfile << 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z': 9;
outfile << 9;
break;

}

}
return 0;

}
Last edited on

1
2
3
4
5
6
7
int main() {
int counter=0;
...
// After every outfile << x;
counter++;
if (counter >=7) 
{cout "\n"; counter=0;}
That isn't working. After the counter hits 8, it just outputs 1 number per line and does a newline after each. And for some reason, I'm not even getting the correct output. This is really frustrating.
Edit: i forgot to put the counter in the loop, my bad. But for some reason even though i used the toupper function, it's still only outputting numbers for the uppercase letters. :/
Last edited on
char toupper(oldNumber);

This does not do what you think it does. I think you are actually declaring a char variable with the name toupper and assigning it oldNumber as an initial value. (I'm not positive why there is not a name collision between your variable toupper and the function name toupper.)

toupper does not change the value of oldNumber. Instead, it returns the uppercase value of the value passed in. You have to either store the return value in a variable (char newNumber = toupper(oldNumber); switch (newNumber)) or operate on the returned value (switch (toupper(oldNumber))) to get the effect you are desiring.

@doug4, you're right- it's declaring a variable named toupper. It does collide with ::toupper(), but it shadows the name. (You would have to qualify ::toupper to name the function)
Last edited on
closed account (E0p9LyTq)
@dhanken44,

First things first....PLEASE learn to use code tags, it makes it easier to read your code and easier to point out line(s) of code where there are problems.
http://www.cplusplus.com/articles/jEywvCM9/

(Hint, you can go back and edit your post to add code tags)

Now, to the meat of your question. You can use the % (modulus) operator to count when to add a newline character. Following is a slimmed down version of how you might code the logic:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main ()
{
   for (auto loop = 1; loop <= 25; loop++)
   {
      std::cout << loop << "\t";
      
      if (loop % 7 == 0)
      {
         std::cout << "\n";
      }
   }
}

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


How you read the data from your file and mash it all together to make your phone number output is up to you.
@mbozzi,

Thank-you for the gentle reminder. [palm to forehead] Of course the toupper declaration is inside a function scope and won't collide with the name of the function that was intended.

I want to plead temporary insanity and throw myself on the mercy of this forum. My insanity was induced by the distraction of hard-to-reproduce bootup problems I'm encountering at work. (I think I am going to become quite intimate with u-boot code in the not-to-distant future.) Also, I will try to blame the lack of indentation in the original post as confusing me. (Maybe not the most convincing of arguments.) In my temporary insanity, I think I was confusing the standard function name "toupper" with a reserved word. I think I need more sleep.
I want to plead temporary insanity and throw myself on the mercy of this forum. My insanity was induced by the distraction of hard-to-reproduce bootup problems I'm encountering at work. (I think I am going to become quite intimate with u-boot code in the not-to-distant future.) Also, I will try to blame the lack of indentation in the original post as confusing me. (Maybe not the most convincing of arguments.) In my temporary insanity, I think I was confusing the standard function name "toupper" with a reserved word. I think I need more sleep.

Or, y'know, you're human like the rest if us :P
Topic archived. No new replies allowed.