I really need help with this project.
1) Write a program that uses a switch program structure
2) Upon user input your switch structure shall calculate and display one of the three loops for loop, while loop, and do-while loop calculations.

Dani AI

Generated

As and already noted, the usual, safe approach for this assignment is to use an integral menu value in the switch and keep each case small. The standard rules for switch (which require integral/enum types or types convertible to integral) and the need for break to avoid fall-through are described here: switch statement - cppreference.com. A clean design is: read a numeric choice into an int, read the problem input (for example n), then switch on the choice and call one of three functions that implement the same calculation with for, while, and do-while.

The code below shows that pattern using a simple calculation (sum of 1..n). It illustrates input validation, a compact switch, and three loop implementations kept in separate functions so each case remains tiny and testable.

#include <iostream>

int sumFor(int n) {
    int s = 0;
    for (int i = 1; i <= n; ++i) s += i;
    return s;
}

int sumWhile(int n) {
    int s = 0;
    int i = 1;
    while (i <= n) { s += i; ++i; }
    return s;
}

int sumDoWhile(int n) {
    int s = 0;
    int i = 1;
    if (n <= 0) return 0;
    do { s += i; ++i; } while (i <= n);
    return s;
}

int main() {
    int choice = 0, n = 0;
    std::cout << "Choose loop (1=for, 2=while, 3=do-while): ";
    if (!(std::cin >> choice)) return 1;
    std::cout << "Enter n: ";
    if (!(std::cin >> n)) return 1;
    switch (choice) {
        case 1: std::cout << sumFor(n) << '\n'; break;
        case 2: std::cout << sumWhile(n) << '\n'; break;
        case 3: std::cout << sumDoWhile(n) << '\n'; break;
        default: std::cerr << "Invalid choice\n"; break;
    }
    return 0;
}

Notes and common pitfalls: keep switch cases short (call functions rather than stuffing logic into cases), always handle invalid input from std::cin before switching, and remember break unless intentional fall-through is required. As implied, posting attempted code and the exact compile/runtime error will get faster, targeted help. For concise references on loop syntax see for statement - cppreference.com and while statement - cppreference.com.

Recommended Answers

All 3 Replies

one piece of advice switch statements will only accept built in types of C++. gl.

one piece of advice switch statements will only accept built in types of C++. gl.

Yep - a switch statement will not, for example, accept a string variable for comparison. It will, however, accept a char or int variable type

Another thing that's easy to miss or overlook is to remember to put "break;" at the end of each case, otherwise it'll also continue executing what's in the following cases (unless this is something you specifically want to happen)

For more information on switch statements, or the various loops, i recommend searching on google. There's a lot of information available by searching (for example) "c++ switch statement example" or "c++ for loop example", etc

If you have any more questions after searching some, by all means post up and we'll help you out with it

I really need help with this project.
1) Write a program that uses a switch program structure
2) Upon user input your switch structure shall calculate and display one of the three loops for loop, while loop, and do-while loop calculations.

smells like an assignment?
If so show efforts before ask for help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.