This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++] loop, string or char* help!  (Read 5682 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++] loop, string or char* help!
« on: 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

Offline 10n1z3d

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
    • View Profile
Re: [C++] loop, string or char* help!
« Reply #1 on: April 14, 2011, 09:55:12 AM »
If I did understand the problem correctly this is how it might look like:

Code: [Select]
#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++  :)
Code: [Select]
python -c "print ''.join(chr(x) for x in [int(oct(39)) + 2, 24 * 2, 313 % 203, 0x31, (2 ** 7) - 6, int('051'), (3 << 6) - 92])"

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: [C++] loop, string or char* help!
« Reply #2 on: April 14, 2011, 09:56:54 AM »
Isent that as easy as cin >> yourarray[0]; That is if yourarray is an array of char arrays or a string array.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C++] loop, string or char* help!
« Reply #3 on: April 14, 2011, 06:57:54 PM »
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
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C++] loop, string or char* help!
« Reply #4 on: April 15, 2011, 07:57:57 AM »
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.
-Xires

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] loop, string or char* help!
« Reply #5 on: April 15, 2011, 11:33:13 AM »
the book is this http://www.mediafire.com/?dzzmhzbn2to

that exercise is on page 229 if Im not wrong.

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C++] loop, string or char* help!
« Reply #6 on: April 15, 2011, 05:44:12 PM »
So do you understand this problem now enough to create your own solution or do you still need help with it?
-Xires

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] loop, string or char* help!
« Reply #7 on: April 15, 2011, 09:33:55 PM »
yes I need help with this cause the next problems are alike  :'(
well I think that the answer is close to this:

Code: [Select]
#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:
Code: [Select]
#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;
}
 

« Last Edit: April 15, 2011, 09:54:56 PM by gh0st »

Offline 10n1z3d

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
    • View Profile
Re: [C++] loop, string or char* help!
« Reply #8 on: April 16, 2011, 12:10:55 PM »
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.
« Last Edit: April 16, 2011, 12:12:12 PM by 10n1z3d »
Code: [Select]
python -c "print ''.join(chr(x) for x in [int(oct(39)) + 2, 24 * 2, 313 % 203, 0x31, (2 ** 7) - 6, int('051'), (3 << 6) - 92])"

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.