Brauche ein wenig c++ Hilfe



  • Hallo leute bin neu im board.Ich studiere c++ in englishem und ich brauche hilfe für ein paar codes die ich hier listen werde es sind 6 problem aufgaben die ich leider nicht ganz so richtig verstehe und die muss ich heute abschicken,kann mir bitte jemand mit den 6 fragen helfen ? Falls ihr english versteht ist das gut 🙂

    Es ist mir sehr wichtig da ich heute die marks bekomme für die aufgaben,sollte ich es nicht schaffen bekomme ich keine marks 😞

    Hoffe ihr c++ leute könnt mir bitte helfen 🙂

    Hier sind die aufgaben:

    2. Write a program using a while loop that asks the user to enter their sales for each month until they enter -1 (the sentinel value - or anything negative). After this, it should tell the user how many months, the total sales and the average sales.
    If the user enters a negative value, it should not cause an error. Make your program able to produce the output below.

    Set the decimal notation to fixed and the precision to 2 decimal places for currency and display a $ sign with each result.
    Remember that a while loop requires a priming read before the while clause.
    (Note that if you have done your tutorial work for this week, you will see some similarities to code you have already written. Remember to try and re-use your code if it is appropriate.)

    Make sure you tackle this problem in steps. Get one part to work, then add code to get the next part to work or add complexity.
    Remember: To set the decimal format to 2 decimal places for currency:
    #include <iomanip>
    using std::ios;
    using std::setiosflags;
    using std::setprecision;
    Then set fixed notation and a precision of 2:
    cout << setiosflags(ios::fixed) << setprecision(2);
    Sample Output:
    Please enter your sales for month 1: 100
    Please enter your sales for month 2: 200
    Please enter your sales for month 3: 0
    Please enter your sales for month 4: 200
    Please enter your sales for month 5: -1

    For 4 months:

    Total sales: $500.00
    Average sales: $125.00
    Press any key to continue . . .

    and - another run:

    Please enter your sales for month 1: -1

    For 0 months:

    Total sales: $0.00
    Average sales: $0.00
    Press any key to continue . . .

    3. Write another version of the above program to use a do-while loop:
    o Close the solution and create a new project
    o Open the previous code file and copy the code, pasting it into a new .cpp file
    o Change the program so that it uses a do-while loop instead of a while loop
    o Remove the priming read and get the user input inside the loop, at the top of the loop body
    o Then use an if statement to determine whether to execute the remaining code in the loop body

    4. Write a program using a do-while loop that prompts the user for a number between 1 and 100 (inclusive).
    If the number is invalid it is to display a message telling the user that the number is invalid and loop so that the user is prompted to enter the number again.
    When the number is valid it is to exit the loop and display the number.

    5. Write a program to calculate the average sales for a store based on the average sales of each staff member:
    o The program will prompt for each sale person's name and then prompt for the average sales for that person
    o Use the string data type and the getline() function to get the name
    o For each sales person the program will display a message like:

    The average sales for Sally Jones is $200.15
    o When the word "quit" is entered as the name, the program is to calculate the average store sales then exit
    o Again, set the decimal notation to fixed and the precision to 2 decimal places for currency and display a $ sign with each result
    o The program is to display the final average store sales with an appropriate message on exit
    o For the string data type:
    #include<string>
    using std::string;
    o Declaring a string variable and using getline()
    string name;
    getline(cin, name)

    6. Write a program to generate a random number between 1 and 100.
    o Add a for loop to generate 5 random numbers (only after you've got a working program that generates 1 number)
    o Each time through the loop, the program is to display a message like that below:
    Random number 1 is .....
    Random number 2 is .....
    7. The header file required is:
    8. #include <ctime>
    9. The function calls are:
    10. srand(time(0)); // the seed - remember to seed once only in the program
    11. num = lower_limit + rand() % (upper_limit - lower_limit + 1);

    Danke

    Presszero



  • und woran hakt es genau?
    kriegst Du keine Zeile hin, dann hast Du ganz andere Probleme als diese 6 😃



  • (Note that if you have done your tutorial work for this week, you will see some similarities to code you have already written. Remember to try and re-use your code if it is appropriate.)

    Hast du das gemacht? Wenn ja, dann schau dir das doch nochmal an. Dann versuch dich selber und schreib hier nochmal rein.

    Wenn du es erstmal selbst versucht hast, werden dir auch mehr Leute helfen. Ganz selber schreiben hab ich auch kein Lust.



  • Hi leut

    Ok habe versucht problem 2 zu machen soweit und bin aus diesem ergebnis rausgekommen.

    Problem 2:

    2. Write a program using a while loop that asks the user to enter their sales for each month until they enter -1 (the sentinel value - or anything negative). After this, it should tell the user how many months, the total sales and the average sales.
    If the user enters a negative value, it should not cause an error. Make your program able to produce the output below.

    Set the decimal notation to fixed and the precision to 2 decimal places for currency and display a $ sign with each result.
    Remember that a while loop requires a priming read before the while clause.
    (Note that if you have done your tutorial work for this week, you will see some similarities to code you have already written. Remember to try and re-use your code if it is appropriate.)

    Make sure you tackle this problem in steps. Get one part to work, then add code to get the next part to work or add complexity.
    Remember: To set the decimal format to 2 decimal places for currency:
    #include <iomanip>
    using std::ios;
    using std::setiosflags;
    using std::setprecision;
    Then set fixed notation and a precision of 2:
    cout << setiosflags(ios::fixed) << setprecision(2);
    Sample Output:
    Please enter your sales for month 1: 100
    Please enter your sales for month 2: 200
    Please enter your sales for month 3: 0
    Please enter your sales for month 4: 200
    Please enter your sales for month 5: -1

    For 4 months:

    Total sales: $500.00
    Average sales: $125.00
    Press any key to continue . . .

    and - another run:

    Please enter your sales for month 1: -1

    For 0 months:

    Total sales: $0.00
    Average sales: $0.00
    Press any key to continue . . .

    Lösung die ich geschrieben habe :

    #include <iostream.h>
    //#include <iomanip>
    //using namespace std;

    int main()
    {
    int sales = 0;
    int sum = 0;
    double averagy = 0.0;
    int i = 0;

    //cout <<setiosflags(std::fixed)<<setprecision(2)<<averagy;

    do
    {
    for (i=1;i<=12;i++)
    {
    cout <<"Please enter your sales for month"<<i<<":"<<" ";
    cin >> sales;
    sum = sum + sales;

    }
    } while (sales >= 0);

    averagy = sum/i;
    cout <<""<<endl;
    cout <<"for "<<i<<" months:"<<endl;
    cout <<""<<endl;
    cout <<"Total Sales: $"<<sum<<endl;
    cout <<"Average sales: $"<<averagy<<endl;

    return 0;
    }



  • Erstmal ist die for()-Schleife da drin überflüssig, schließlich willst du jeden Monat einzeln abfragen und nicht in 12er-Paketen. Stattdessen solltest du i innerhalb deiner Schleife explizit hochzählen.

    Zweitens war explizit eine while()-Schleife in der Aufgabe gefordert - do-while() ist Aufgabe 3 😉

    Und drittens fehlt noch eine Abfrage, um Division durch Null zu verhindern (für den Fall, daß der User sofort abbricht - siehe Beispieldurchgang 2).



  • oh mensch >(

    ich glaube nicht das ich das mal hinbekommen werde,ich habe naechste woche einen test und ich bin mir nicht so sicher das ich schaffen werde. Immo sind wir bei den loops usw..und am anfang hatte ich die uebersicht doch nun habe ich die uebersicht verloren und weiss nun leider nicht mehr was ich machen soll.

    Wie sehen denn die anderen aufgaben aus in etwa koennte mir da jemand helfen??Gibt es online seiten wo ich gucken kann damit ich das besser in den griff bekomme???irgendwelche seiten zum arbeiten und lernen ?

    danke



  • Auf dieser Seite gibts sogar eine Tutorial-Sektion:
    http://www.c-plusplus.net/cms/modules.php?op=modload&name=Downloads&file=index


Anmelden zum Antworten