EvilZone
Programming and Scripting => C - C++ => : gh0st April 14, 2011, 01:40:38 AM
-
hello again guys well this is the problem:
4. You sell the book C++ for Fools. Write a program that has you enter a year’s worth of
monthly sales (in terms of number of books, not of money). The program should use a
loop to prompt you by month, using an array of char * (or an array of string objects, if
you prefer) initialized to the month strings and storing the input data in an array of int.
Then, the program should find the sum of the array contents and report the total sales
for the year.
well I just get lost at "us loop to prompt you by month" well I underestand that what they want is to use loop to ask for the input in all the months for example looping would output : "Enter the amount of books of january: " and like this every month but I was declaring the arrays and strings but Ive no idea how to use loop to ask for the input however please someone help me. :-\
awards: +2 of karma
-
If I did understand the problem correctly this is how it might look like:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int i, sales[12], total_sales = 0;
const char *months[] = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"};
for (i=0; i<12; i++) {
printf("Enter the number of books sold in %s: ", months[i]);
scanf("%d", &sales[i]);
total_sales += sales[i];
}
printf("Total sales: %d\n", total_sales);
return EXIT_SUCCESS;
}
It's in C but i think you could easily convert it to C++ :)
-
Isent that as easy as cin >> yourarray[0]; That is if yourarray is an array of char arrays or a string array.
-
Isent that as easy as cin >> yourarray[0]; That is if yourarray is an array of char arrays or a string array.
Yes it is, although cin will not work with spaces and will not limit the input so if it is only a char then it will overflow into the next part of the array :P
-
gh0st; what book are you trying to learn from? This isn't a class assignment, is it? Please don't take this the wrong way but there seems to be some fundamental flaw in your understanding of the language and it appears to cause random issues on a daily basis. Is there something that you are not quite understanding about the language? Perhaps if we can narrow such an issue down, we can begin to assist you in that area thus making it far easier for you to continue learning and perhaps at an accelerated pace.
-
the book is this http://www.mediafire.com/?dzzmhzbn2to (http://www.mediafire.com/?dzzmhzbn2to)
that exercise is on page 229 if Im not wrong.
-
So do you understand this problem now enough to create your own solution or do you still need help with it?
-
yes I need help with this cause the next problems are alike :'(
well I think that the answer is close to this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int year;
char * months[12]={enero,febrero,marzo,abril,mayo,junio,julio,agosto,septiembre,octubre,noviembre,diciemre};
for (i=0;i<12;i++)
cout << i[months];
cin.get[i](month[1],1);
cout << months[size];
cout << endl;
system("pause");
return 0;
}
the answer may look like this too:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char january,February,march,april,may,june,july,august,september,october,noviember,december;
int year;
char months[12]={january,February,march,april,may,june,july,august,september,october,noviember,december};
char c = c;
do
{
cout << "in january: " << cin.getline(january,1);
cout << "in february: " << cin.getline(february,2);
cout << "in march: " << cin.getline(march,3);
cout << "in april: " << cin.getline(april,4);
cout << "in may: " << cin.getline(may,5);
cout << "in june: " << cin.getline(june,6);
cout << "in july: " << cin.getline(july,7);
cout << "in august: " << cin.getline(august,8);
cout << "in september: " << cin.getline(september,9);
cout << "in october: " << cin.getline(october,10);
cout << "in november: " << cin.getline(november,11);
cout << "in december: " << cin.getline(december,12);
cout << "type C to do the sum: " << cin c;
} while (c = c)
cout << january+February+march+april+may+june+july+august+september+october+noviember+december;
cout << endl;
system("pause");
return 0;
}
-
Dude, lol, I don't think this would even compile. I suggest you to to revisit the chapter about Arrays. And if you really want to learn C++ you should get some books on PAPER translated in your native language (Spanish?). This way it would be a lot easier for you.