C++ Question

This is a discussion on C++ Question within the C++ Programming forums, part of the General Programming Boards category; Hello, i got a problem with this question with my C++ lab reports, i was wondering if anyone could tell ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    C++ Question

    Hello, i got a problem with this question with my C++ lab reports, i was wondering if anyone could tell me why it isn't working properly, the code is:

    Lab Exercise 3a

    What output will be produced by the following code when embedded in a complete program?
    Consider initial values for first_choice of 0, 1 and -1.

    Code:
    int x = first_choice++;
    switch (first_choice + x)
    {
      case 1: cout << "Waitrose\n" break;
    
      case 2: cout << "Sainsburys\n" break;
    
      case 3: cout << "Tesco\n" break;
    
      case 4: cout << "Asda\n" break;
    
      default: cout << "No shopping today!\n";
    }
    So i made this to figure out the answer:
    Code:
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    int x = first_choice++;
    switch (first_choice + x)
    {
      case 1: cout << "Waitrose\n" break;
    
      case 2: cout << "Sainsburys\n" break;
    
      case 3: cout << "Tesco\n" break;
    
      case 4: cout << "Asda\n" break;
    
      default: cout << "No shopping today!\n";
    }
    
    return 0;
    
    }
    Anyone know why, i get illegal case and illegal break errors.

    Thanks

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,821
    I think the intent of your lab question was that you be the computer and reason the output for yourself. The question mentions that the code provided is not a complete program, so you'll have to revise the code accordingly if you're too lazy to think it through.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    every statement must have a semicolon after it, so "cout << "Waitrose\n" break;" is considered one statement (which doesnt make sense). after your cout statement (before break) you need to tell the compiler that the statement is done, by inserting a semicolon.

    it is actually easier to follow the example without doing any coding. there is no trick involved. for example, if first_choice is 0, x will be 0, so their sum is 0, and there is no case for this so it will execute the default case.

  4. #4
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Quote Originally Posted by nadroj View Post
    for example, if first_choice is 0, x will be 0, so their sum is 0, and there is no case for this so it will execute the default case.
    Because of the first_choice++, first_choice will have been incremented when it gets to the switch statement, so:

    x + first_choice = 0 + 1 = 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21