Help please

Pages: 12
Ok so in my class(I am a computer science student) our teacher gave us this assignment

Write an application where you ask the user to input the price per letter (PPL), and then ask the user to input the sentence they want printed. The application should then calculate the number of letters and give the user the total cost in the following manner:

You have 40 letters at $3.45 per letter, and your total is $138.00.

[Important] Your application should only use "FOR" loops. Do not use any String or Array functions.

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 "stdafx.h"
#include<iostream>
#include<string>
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{

	float price;
	char sent;
	float times;

	cout<<"Please enter the price PPL\n";
	cin>>price;

	cout<<"Please enter your sentence\n";
	

	for(char i =1;i < 10;i++)
	{
		cin>>sent;
		

	}


That is all I have at this time, I have tried a few things but I dont want no one to do it for me but if you could guide me in the right direction. I don't understand how I can do it with out using string or an array.
i tried to use strlen but it does not work
Are you allowed to use kbhit() and/or getch()?
Yeah he didnt say we wasnt allowed to use anything but string or array functions bro I assume we can use anything else im stumped so bad on this
closed account (j3Rz8vqX)

My perspective and Ideas -- PSEUDO CODE:
Prompt the user for the price per letter.
Get the price per letter (float value).

Prompt the user for a sentence.
Get the sentence (overflow numerous characters, followed by a return - try it)?

The for loop should increment by a value of "price per letter".

Since loop is controlled by your loops max repetition, possibly use a break within an if condition to test if character entered was '.'?
This is almost assuredly not what your teacher/professor wants. I think if anything he would want you to read into a character array. But this is a way using getchar(). Its archaic, but I think it works...

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
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int amount = -1; //This is -1 to counteract the "Enter" character entered
    char test; 
    
    //Ask for PPL input

	do
	{
	    test = getchar(); //Reads anything the user presses
	    if(test == ' ') //Skips the spaces
	    {
	        //Do nothing
	    }
	    else //A character is given and will be charged
	    {
	        amount++;
        }

	}while(test != '\n');

	//Compute the amount of money charged here
	
	cin.ignore();
}
I am so confused, this is only our 4th week in the class and the book has nothing to do with this, I dont get why he wont let us use an array or string function or something

Try this:

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

#include <iostream>
#include <conio.h>
using namespace std;

double readLetters(double price);

int main()
{
	double price, result;

	cout << "Enter PPL: ";
	cin >> price;

	cout << "Enter Letters and press <ENTER> to finish: ";
	result = readLetters(price);

	cout.precision(4); 
	cout << endl << "Result: $" << result;
	return 0;
}

double readLetters(double price)
{
	char ch;
	double total = 0;
	while ((ch = _getch()) != 13)
	{
		cout << ch;
		total += price;
	}
	return total;
}



If you want to ignore spaces in the calculations make these modifications to readLetters() :

1
2
3
4
5
6
7
8
9
10
11
12
double readLetters(double price)
{
	char ch;
	double total = 0;
	while ((ch = _getch()) != 13)
	{
		if (ch != 32)  // ignore space in totals.			
			total += price;
		cout << ch;
	}
	return total;
}
Last edited on
@Softrix, I don't think adiktid is going to be allowed to use something like getchar. Do you know of a way to read into a character array? I don't, as I have always just used strings, but I was hoping you might.
Im sure he said that he couldnt use array or strings?

Yeah he didnt say we wasnt allowed to use anything but string or array functions bro I assume we can use anything else im stumped so bad on this


He said array functions, which could mean previously defined functions, but I have trouble believing he couldn't use arrays himself. Then again, that could very well be the case.
I am going to try this, I really appreciate the help if anyone else can think of something please let me know...I just dont get what not using strings or arrays to do something like this really teaches me lol

Well to hold a sentence would require a array or string, or failing that, to achieve what your asking would require reading on a letter by letter basis like in my example.

However, the _getch() function is usually frowned upon these days. I dont see any other way if he's telling you not to use strings or arrays - this seems an odd exercise.

ahhh ok he said we can use strings we just cant get the length using string or array functions
Now thats different lol
Softrix I am sorry bro lol I still don't get ti though, I don;t understand how I would do it and the chapters we are supposed to read ONLY tells us about the FOR loop so im like wtf

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

#include <iostream>
#include <string>
using namespace std;

int main()
{

	string mySentence;
	
	double price, result = 0;
	int index = 0;

	cout << "Enter PPL: ";
	cin >> price;

	cout << "Enter Sentence:  ";
	cin >> mySentence;

	while (mySentence[index] != 0)
	{
		if (mySentence[index] >= 'a' && mySentence[index] <= 'z' ||
			mySentence[index] >= 'A' && mySentence[index] <= 'Z')
			result += price;
		index++;
	}

	cout << "Total:  " << result;

	return 0;
}
Last edited on
Does it specify FOR loop or just a LOOP in general?
closed account (j3Rz8vqX)
Possible spoiler @ pastie:
http://pastie.org/9118312
Last edited on
[Important] Your application should only use "FOR" loops. Do not use any String or Array functions.

Pages: 12