13 Replies - 1814 Views - Last Post: 06 September 2009 - 09:31 PM Rate Topic: -----

#1 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 07:26 PM

Thread title is probably confusing, but I tried my best.

My exercise problem states (It's a longy):

"Suppose that the file inData.txt contains the following data:
10.20 5.35
15.6
Randy Gill 31
18500 3.5
A

The numbers in the first line represent the length and width, respectively, of a rectangle. The number in the second line represents the radius of a circle. The third line contains the first name, last name, and the age of a person. The first number in the fourth line is the savings account balance at the beginning of the month and the second number is the interest rate per year. (Assume that PI = 3.1416.) The fifth line contains an uppercase letter between A and Y (inclusive). Write statements so that after the program executes, the contents of the file outData.txt are shown below. If necessary, declare additional variables. Your statements should be general enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results.

'Rectangle:
Length = 10.20, width = 5.35, area = 54.57, parameter = 31.10

Circle:
Radius = 15.60, area = 764.54, circumference = 98.02

Name: RandyGill, age: 31
Beginning balance = 18500.00, interest rate = 3.50%
Balance at the end of the month = $19147.50

The character that comes after A in the ASCII set is B' "

I have everything down except the last line of the results. I can't figure out how to get the program to insert a letter that comes after the one that was placed before it. Here's my code so far

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

const double PI = 3.1416;

int main()
{
	//File Stream Declaration
	ifstream inFile;
	ofstream outFile;

	//Variable declaration
	double length, width, areaRec, parameter; //Variables for rectangle
	double radius, areaCirc, circumference; //Variables for circle
	double begBal, intRate, endBal; //Beginning balance, interest rate, end balance
	int age;
	char ch;
	
	string name1, name2;

	//Opening files
	inFile.open("c:\\inData.txt");
	outFile.open("c:\\outData.txt");

	outFile << fixed << showpoint << setprecision(2); //Use decimals to two places

	cout << "Processing Data. . ." << endl;

	inFile >> length >> width >> radius
		>> name1 >> name2 >> age >> begBal
		>> intRate >> ch;

	//Inserting data in following variables
	parameter = length * 2 + width * 2;
	areaRec = length * width;
	areaCirc = pow(radius, 2) * PI;
	circumference = (radius * 2) * PI;
	endBal = (begBal * (intRate / 100)) / 12 + begBal;

	outFile << "Rectangle:" << endl   //Rectangle start
		<< "Length = " << length << ", "
		<< "width = " << width << ", "
		<< "area = " << areaRec << ", "
		<< "parameter = " << parameter << endl
		<< endl
		<< "Circle:" << endl		  //Circle start
		<< "Radius = " << radius << ", "
		<< "area = " << areaCirc << ", "
		<< "circumference = " << circumference << endl
		<< endl
		<< "Name: " << name1 << name2 << ", " //Name and everything else start
		<< "age: " << age << endl
		<< "Beginning balance = " << begBal << ", "
		<< "interest rate = " << intRate << "%" << endl
		<< "Balance at the end of the month = $" << endBal << endl
		<< endl;

	inFile.close();
	outFile.close();

	system("pause");

	return 0;
}


I'd appreciate comments on how to organize it better. Should I have done multiple outFile commands to seperate it, or was it fine how I did it? Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting the letter that comes after 'enter letter here'

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 07:50 PM

Here's a hint on this: http://web.cs.mun.ca...scii-table.html.
Was This Post Helpful? 1
  • +
  • -

#3 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:02 PM

I appreciate the gesture, but I really have no clue what that chart is even saying, let alone how to use it to help myself.

I've used ~90% of the commands I've learned in that program so far, and I'm not sure exactly how I can use the other ~10% in it. I assume I need to use the "get" command, but I've only learned how to use the get command to get a character that's already in the inFile. I'm only on the third chapter of my book.
Was This Post Helpful? 0
  • +
  • -

#4 azjherben  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 25-March 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:34 PM

I'm not sure of the actual syntax, although you could google it.
What you need to do is convert the letter into a number, and then add one number to that, and convert it back:

Example:

string letter = "a";
string newlett;
int num = lettertonum(letter);
if(num<26){
newlett = numtoletter(num+1);
}
if(num==26){
newlett = "a";
}


Then newlett would be the new letter, here, it'd be "b". Although alphatoint and inttoalpha arn't the exact phrases used.




Here are two sample functions I made. They arn't the right way to do it, but I didn't find the right way on google so here:
(They olny will convert to lower case, 1="a" & 27 = "." & 28 = "." also, "a" = 1 & "A" = 1)

int lettertonum(string letter)
{
int ret = -1;
string clett = tolower(letter);

if(clett == "a"){ret = 1;}
if(clett == "b"){ret = 2;}
if(clett == "c"){ret = 3;}
if(clett == "d"){ret = 4;}
if(clett == "e"){ret = 5;}
if(clett == "f"){ret = 6;}
if(clett == "g"){ret = 7;}
if(clett == "h"){ret = 8;}
if(clett == "i"){ret = 9;}
if(clett == "j"){ret = 10;}
if(clett == "k"){ret = 11;}
if(clett == "l"){ret = 12;}
if(clett == "m"){ret = 13;}
if(clett == "n"){ret = 14;}
if(clett == "o"){ret = 15;}
if(clett == "p"){ret = 16;}
if(clett == "q"){ret = 17;}
if(clett == "r"){ret = 18;}
if(clett == "s"){ret = 19;}
if(clett == "t"){ret = 20;}
if(clett == "u"){ret = 21;}
if(clett == "v"){ret = 22;}
if(clett == "w"){ret = 23;}
if(clett == "x"){ret = 24;}
if(clett == "y"){ret = 25;}
if(clett == "z"){ret = 26;}

return ret;
}

string numtoletter(int num)
{
if(num == 1){return "a";}
if(num == 2){return "b";}
if(num == 3){return "c";}
if(num == 4){return "d";}
if(num == 5){return "e";}
if(num == 6){return "f";}
if(num == 7){return "g";}
if(num == 8){return "h";}
if(num == 9){return "i";}
if(num == 10){return "j";}
if(num == 11){return "k";}
if(num == 12){return "l";}
if(num == 13){return "m";}
if(num == 14){return "n";}
if(num == 15){return "o";}
if(num == 16){return "p";}
if(num == 17){return "q";}
if(num == 18){return "r";}
if(num == 19){return "s";}
if(num == 20){return "t";}
if(num == 21){return "u";}
if(num == 22){return "v";}
if(num == 23){return "w";}
if(num == 24){return "x";}
if(num == 25){return "y";}
if(num == 26){return "z";}
return ".";
}


This post has been edited by azjherben: 06 September 2009 - 08:46 PM

Was This Post Helpful? 1
  • +
  • -

#5 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:45 PM

Thanks, but there's got to be an easier way to do this. I'm trying to keep it to commands I've learned in the book. I'm doing the exercises at the end of chapter 3 in my book and I hope they wouldn't ask me to do something they haven't taught me yet.

Would there be a way to convert a char variable into a number, turn the number into an integer, add 1 to it, then enter that number into another char variable?
Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:49 PM

Here is an easier way:
// after you read in the other data,
// this is the character part
char character = '\0';
file >> character;

cout << "The letter that comes after \'" << character << "\' is \'" << static_cast<char>( (int)character + 1 ) << "\'" << endl;


This post has been edited by eker676: 06 September 2009 - 08:49 PM

Was This Post Helpful? 1
  • +
  • -

#7 azjherben  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 25-March 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:49 PM

Didn't realize others replied, nvm this. Eker's is easier.

In fact, he's a whole program that will ask for a letter, and then return the next one. (It's lowercase olny.)


#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
int lettertonum(string letter)
{
int ret = -1;
string clett = letter;

if(clett == "a"){ret = 1;}
if(clett == "b"){ret = 2;}
if(clett == "c"){ret = 3;}
if(clett == "d"){ret = 4;}
if(clett == "e"){ret = 5;}
if(clett == "f"){ret = 6;}
if(clett == "g"){ret = 7;}
if(clett == "h"){ret = 8;}
if(clett == "i"){ret = 9;}
if(clett == "j"){ret = 10;}
if(clett == "k"){ret = 11;}
if(clett == "l"){ret = 12;}
if(clett == "m"){ret = 13;}
if(clett == "n"){ret = 14;}
if(clett == "o"){ret = 15;}
if(clett == "p"){ret = 16;}
if(clett == "q"){ret = 17;}
if(clett == "r"){ret = 18;}
if(clett == "s"){ret = 19;}
if(clett == "t"){ret = 20;}
if(clett == "u"){ret = 21;}
if(clett == "v"){ret = 22;}
if(clett == "w"){ret = 23;}
if(clett == "x"){ret = 24;}
if(clett == "y"){ret = 25;}
if(clett == "z"){ret = 26;}

return ret;
}

string numtoletter(int num)
{
if(num == 1){return "a";}
if(num == 2){return "b";}
if(num == 3){return "c";}
if(num == 4){return "d";}
if(num == 5){return "e";}
if(num == 6){return "f";}
if(num == 7){return "g";}
if(num == 8){return "h";}
if(num == 9){return "i";}
if(num == 10){return "j";}
if(num == 11){return "k";}
if(num == 12){return "l";}
if(num == 13){return "m";}
if(num == 14){return "n";}
if(num == 15){return "o";}
if(num == 16){return "p";}
if(num == 17){return "q";}
if(num == 18){return "r";}
if(num == 19){return "s";}
if(num == 20){return "t";}
if(num == 21){return "u";}
if(num == 22){return "v";}
if(num == 23){return "w";}
if(num == 24){return "x";}
if(num == 25){return "y";}
if(num == 26){return "z";}
return ".";
}

int main () {

string inletter;

cout<<"Enter a letter:\n";
cin>>inletter;

string newlett;
int num = lettertonum(inletter);
if(num<26){
newlett = numtoletter(num+1);
}
if(num==26){
newlett = "a";
}

cout<<"Returned... "<<newlett<<"\n";

	while(true){Sleep(10);}

}


This post has been edited by azjherben: 06 September 2009 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:50 PM

Look above ^ at my post

This post has been edited by eker676: 06 September 2009 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#9 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 08:57 PM

I actually figured out an easy way to do it, too.

outFile << "Rectangle:" << endl   //Rectangle start
		<< "Length = " << length << ", "
		<< "width = " << width << ", "
		<< "area = " << areaRec << ", "
		<< "parameter = " << parameter << endl
		<< endl
		<< "Circle:" << endl		  //Circle start
		<< "Radius = " << radius << ", "
		<< "area = " << areaCirc << ", "
		<< "circumference = " << circumference << endl
		<< endl
		<< "Name: " << name1 << name2 << ", " //Name and everything else start
		<< "age: " << age << endl
		<< "Beginning balance = " << begBal << ", "
		<< "interest rate = " << intRate << "%" << endl
		<< "Balance at the end of the month = $" << endBal << endl
		<< endl
		<< "The character that comes after " << ch1 << " in the ASCII set is ";

	ch1 = ch1++;

	outFile << ch1;


I was super surprised it worked xD

Thanks for all the replies, guys. They all helped me get on the right track!

edit: And I just now realized I could have done that

<< "The character that comes after " << ch1 << " in the ASCII set is " << ch1++;

/facepalm

edit2: Actually, no. It only works the way I put it in the code. /facepalm2

This post has been edited by Yatta!: 06 September 2009 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#10 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 09:14 PM

It would work correctly if you use the pre-increment operator instead of the post-increment operator.

cout << "The character that comes after " << ch1 << " in the ASCII set is " << ++ch1 << endl;
Was This Post Helpful? 0
  • +
  • -

#11 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 09:17 PM

I actually tried that, but my output was: The character that comes after B in the ASCII set is B

This post has been edited by Yatta!: 06 September 2009 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

#12 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 09:28 PM

Oops, your right. The output stream is not being flushed and when the second ch1 is changed the first is affected also.

My first example doesn't actually change the character it just uses if for a sort of calculation.

This post has been edited by eker676: 06 September 2009 - 09:29 PM

Was This Post Helpful? 0
  • +
  • -

#13 azjherben  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 25-March 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 09:29 PM

<< "The character that comes after " << ch1 << " in the ASCII set is "<< ((char)ch1+=1);
could work, let me try
Was This Post Helpful? 0
  • +
  • -

#14 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Getting the letter that comes after 'enter letter here'

Posted 06 September 2009 - 09:31 PM

Nope it is the same thing
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1