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.
#include "stdafx.h"
#include<iostream>
#include<string>
usingnamespace 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.
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...
#include <iostream>
#include <cstdio>
usingnamespace 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
@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.
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.
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