I wrote this code for a homework problem. The instructions say that there is a list of phone numbers that were reversed (ex. 6463124137, needs to be 7314213646) and the program needs to reverse them back. My professor also included this hint: "Hint: One way to reverse the number would be to accept all the input as strings, create a temporary string of length 10, and then manually change each character of the string (e.g. temp.at(2)=number.at(8); etc.)" I tried doing that but the program isn't working. It is not outputting, and there are a few errors. If anyone could explain to me what I am doing wrong I would really appreciate it.

#include <string>
#include <fstream>

using namespace std;

int main () 
{
	ifstream inFile;
	inFile.open("records.txt");
	
	ofstream outFile;
	outFile.open("corrected.txt");
	
	string num, temp;
	
	while (inFile.good())
	{
		string temp.length(10) //error: expected initializer before '.' token
		inFile >> num >> endl;
		outFile << temp.at(0)=num.at(9) << temp.at(1)=num.at(8) << temp.at(2)=num.at(7) 
                << temp.at(3)=num.at(6) << temp.at(4)=num.at(5) << temp.at(5)=num.at(4) << 
                temp.at(6)=num.at(3) << temp.at(7)=num.at(2) << temp.at(8)=num.at(1) <<
                temp.at(9)=num.at(0) << endl; //error: invalid operands of types 'char' and 
                //unresolved overloaded function type>' to binary 'operator<<'
	}
	
    return 0;
}

Recommended Answers

All 11 Replies

are you sure that the numbers in the file is always 10 digits?

Next to simplify the reversing I think you'll need to use loop and manipulate the counter's value for the index

Yes, they are always 10 digits. They are phone numbers. I'm not sure what you mean by this: "manipulate the counter's value for the index". I was using a while loop.

Use a loop to reverse the numbers into your temp array. It's bad form to to do all your work in one statement like you did. Do one simple step at a time. It's a nightmare to debug the way you have it.

are you saying each digit needs it's own loop?

How would a loop for 1 digit be useful? :icon_rolleyes:

Never mind that. I thought that's what you meant because you said to do a simple step at a time. How would I create a temp array?

I tried to make some sort of array. I doubt I am doing this right. This time there are no errors, the program succeeded and created the file "corrected.txt" but the only value it shows is 0. Here is my code:

#include <string>
#include <fstream>


using namespace std;

int main () 
{
	ifstream inFile;
	inFile.open("records.txt");
	
	ofstream outFile;
	outFile.open("corrected.txt");
	
	int num[9];
	int i=0;
	
	while (inFile.good())
	{
		while (i<9) 
		{
			inFile >> num[i];
			i++;
		}
		
		int temp=0;
		i=0;
	
		while (i<9) 
		{
			string num, temp;
			temp[0]=num[9];
			temp[1]=num[8];
			temp[2]=num[7];
			temp[3]=num[6];
			temp[4]=num[5];
			temp[5]=num[4];
			temp[6]=num[3];
			temp[7]=num[2];
			temp[8]=num[1];
			temp[9]=num[0];
			i++;
		}
		
		outFile << temp << endl;
	}
	
    return 0;
}

Print your program.
Sit at a desk with the program and a pencil.
'Execute' your program line by line, loop by loop.
Write down the variable names.
Write down the contents of each variable as they change.

Honestly, I have never done anything like that. We never learn how to do that in class, we usually just type everything in exactly the way the professor does it. Which is probably why I am always so confused when the homework is really different than what we did in class. If I looked at this program I would have no idea what anything does, except for the opening of the files, and returning an integer because main is an integer. But I ended up writing a program that worked, so for now it's good. Although I probably should learn what each line does.

Honestly, I have never done anything like that.

So? What's your point? You never programed before either. in your other classes have you never thought of a technique to help you get your homework done that wasn't directly taught?

We never learn how to do that in class, we usually just type everything in exactly the way the professor does it. Which is probably why I am always so confused when the homework is really different than what we did in class.

Have you ever taken a class before where you had to think? Not just regurgitate information a talking head tells you? Well, in programming you are required to think.

If I looked at this program I would have no idea what anything does, except for the opening of the files, and returning an integer because main is an integer.

Then why are you writing all that other stuff? If you don't understand what you're writing, how do you expect to get the program running?

But I ended up writing a program that worked, so for now it's good.

I doubt it

Although I probably should learn what each line does.

Ya think?

Sorry if this sounds harsh, but you've got to start learning what he's teaching you. You can't fix a car without knowing what you are doing. You can't wire a stereo without thinking about where the wires go. And you can't program without thinking about what you are doing and what you want to accomplish.

You are absolutely right.

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.