strings

This is a program I wrote for a homework problem. It said a list of telephone numbers had been reversed and we need to write a program to switch them back.
My professor included this hint in the instructions: 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 something like that but it isn't working. Here is what I have:
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
#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;
}
Last edited on
The quickest way is to just print each char in the string in a for loop. Luckily, in C++ the string class is pretty amazing. But in C it would be the same since all strings are just char arrays. You would just have to keep track of the size like with any array.


Read up on the string class and actually learn why this works. Try to understand why you type
#include <string> in your code because there are many other methods you can use to solve problems.

1
2
3
4
5
6
7
8
string ReverseString(string word)
{
     string tmp;
     for (int i = word.size(); i > 0; i--)
          tmp += word[i];
          
     return tmp;  
}


Make this a function that you pass each string into. Just use a simple loop to call the function with entry.
Last edited on
@IceThatJaw - your code overflows at its first iteration. strings are indexed from zero through to size() - 1, and your loop starts with int i = word.size()

I'm not really understanding any of this. @IceThatJaw when I included your code in mine the output file just said "dro". I'm assuming it took "word" and reversed it and somehow lost the w. But I need the program to reverse a list of phone numbers.
This should help a lot.

Not sure if this is what you want
but what it does is.
Say you have ...

records.txt
0123456789


This code will change it to

corrected.txt
9876543210


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
#include <string>
#include <fstream>


using namespace std;

int main () 
{
	ifstream inFile;
	inFile.open("records.txt");
	
	ofstream outFile;
	outFile.open("corrected.txt");
	
	while (!inFile.eof())
	{
		string num="", temp="";
		inFile >> num;
		temp =num;
		int j = num.length()-1;
		int length = num.length();
		for (int i = 0; i <length ;i++)
		{
			temp[i] = num[j];
			outFile<<temp[i];
			j--;
		}
		outFile<<endl;

	}
	inFile.close();
	outFile.close();
        return 0;
}
Topic archived. No new replies allowed.