Author Topic: What's wrong with my code  (Read 219 times)

0 Members and 1 Guest are viewing this topic.

Offline Conker

  • Recon Newbie
  • *
  • Posts: 7
  • Reputation: 0
    • View Profile
What's wrong with my code
« on: January 29, 2010, 06:35:08 AM »
Code: [Select]
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


struct soda{
    double price;
}   Grape,Cherry,Vanilla;


    int main()
{
        char input[100];
        char GrapeC[100] = "Grape";
        char CherryC[100] = "Cherry";
        char VanillaC[100] = "Vanilla";
        int loop = 0;
        Grape.price = 1.25;
        Cherry.price = 1.50;
        Vanilla.price = 1.25;

        cout << "List of Sodas.\n";
        cout << "Grape\n" << "Vanilla\n" << "Cherry\n";
        cout << "Which soda would you like to know the cost of?\n";
        cin >> input;

            if (input == GrapeC)
{
                cout << "The grape soda cost: " << Grape.price;

            else if (input == CherryC)

                cout << "The cherry soda cost: " << Cherry.price;

            else if (input == VanillaC)

                cout << "The vanilla soda cost: " << Vanilla.price;

            else{

                cout << "***ERROR***";
}

}

}

It gives me these errors:

Line 33 Error: Expected Primary Expression Before "Else"
Line 33 Error: Expected ; Before "Else"
Line 37 Error: Expected Primary Expression Before "Else"
Line 37 Error: Expected ; Before "Else"
Line 41 Error: Expected Primary Expression Before "Else"
Line 41 Error: Expected ; Before "Else"


I can't figure out whats wrong with it.  :'(

Recon Networks

What's wrong with my code
« on: January 29, 2010, 06:35:08 AM »

Offline Drunken.Canadian

  • Administrator
  • Administrator
  • Recon Spambot
  • *****
  • Posts: 442
  • Reputation: 270
  • ASM, C++, and C# programmer
    • View Profile
    • Infinity Logic
Re: What's wrong with my code
« Reply #1 on: January 29, 2010, 09:48:54 AM »
Here you go mate :D problem solved
Code: [Select]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct soda
{
    double price;
}   Grape,Cherry,Vanilla;

void main()
{
        char input[100];
        char GrapeC[]   = "Grape";
        char CherryC[]  = "Cherry";
        char VanillaC[] = "Vanilla";
        Grape.price      = 1.25;
        Cherry.price     = 1.50;
        Vanilla.price    = 1.25;

        cout << "List of Sodas." << endl;
cout << "+---------+" << endl;
cout << "| Grape   |" << endl;
cout << "| Vanilla |" << endl;
cout << "| Cherry  |" << endl;
cout << "+---------+" << endl << endl;

        cout << "Which soda would you like to know the cost of?" << endl;
cout << "Choice: _______\b\b\b\b\b\b\b";
        cin  >> input;
cout << endl;

            if (strcmp(input, GrapeC) == 0)
                cout << "The grape soda cost: " << Grape.price;

            else if (strcmp(input, CherryC) == 0)
                cout << "The cherry soda cost: " << Cherry.price;

            else if (strcmp(input, VanillaC) == 0)
                cout << "The vanilla soda cost: " << Vanilla.price;

            else
                cout << "***ERROR***";

cin.get();
cin.get();
}
Visit Infinity Logic for up to date programming tutorials.


Offline Conker

  • Recon Newbie
  • *
  • Posts: 7
  • Reputation: 0
    • View Profile
Re: What's wrong with my code
« Reply #2 on: January 29, 2010, 10:00:01 AM »
Hmmm....It works, but what do these do? ???

(strcmp(input, GrapeC) == 0)

and

\b

and

cin.get()

I'm new :\

Offline k3ystone

  • Recon Contributor
  • Recon Spambot
  • *
  • Posts: 415
  • Reputation: 245
    • View Profile
Re: What's wrong with my code
« Reply #3 on: January 29, 2010, 07:34:28 PM »
rather than just giving him a working program, it would be a much better learning experience if you walk him through each of the changes you made to the code.

the strcmp function compares 2 strings (in this case input and GrapeC) to see if they are the same.  if they are, the function returns 0, and if they are not it will return a number other than zero.  you can google strcmp to see specifically what it outputs if the strings are not equal, but for this program that isn't necessary to know.

\b is what's called an escape character or escape sequence.  there are about a dozen or so different escape characters, as shown in this chart:
Code: [Select]
\a  Bell (beep)
\b     Backspace
\f     Formfeed
\n     Newline
\r     Return
\t     Tab
\\     Backslash
\'     Single quote
\"     Double quote
\xdd   Hexadecimal representation
basically, when C++ reads the string it replaces these with a specific character.  for example, if you execute this line of code:
Code: [Select]
cout << "blah blah \n more blah blahit will show up in the terminal as
Code: [Select]
blah blah
more blah blah
because the \n in the original string was replaced with the newline character.  these can be useful when you need to output something in single or double quotations, because if you tried to execute this:
Code: [Select]
cout << "My favorite quote is "blah blah blah" << endl; you would get all kinds of errors because of the "s.  in RandomHero's code, the \b has the same effect as if you had pressed the backspace key.  (to be honest, I don't know why he did it that way...) 

finally, about cin.get().  basically all this function does is retrieve one character from the input stream.  if you wanted to do something with that character, you could assign it as such:
Code: [Select]
char ch = cin.get();In our case though, we aren't assigning it to anything..so what's the point in including this?  the most common way I see this function used is to keep the console window open after the main part of your program has finished executing.  depending on the compiler that you use, the console window may or may not close upon successful execution of the program.  some compilers (eclipse for example) have a built-in console, so this is not an issue.  but for most compilers the console closes once the program has executed, which can be problematic if you need/want to see the output of the program.  since there is nothing in the input stream when it is called, cin.get() waits until something gets put into the stream (aka the user hits enter, or almost any other key on the keyboard), and then retrieves that character.  again, since we aren't assigning that character value to a variable, it is useless to us..but that's ok.  basically the function makes the program stall out until the user presses a button.

let me know if any of this is confusing, I'd be more than happy to explain it further.  good luck!

Offline Drunken.Canadian

  • Administrator
  • Administrator
  • Recon Spambot
  • *****
  • Posts: 442
  • Reputation: 270
  • ASM, C++, and C# programmer
    • View Profile
    • Infinity Logic
Re: What's wrong with my code
« Reply #4 on: January 31, 2010, 09:01:12 AM »
rather than just giving him a working program, it would be a much better learning experience if you walk him through each of the changes you made to the code.

the strcmp function compares 2 strings...

Thanks man, I didn't have time to explain the code :/ I was in class at the time :P And the \b stuff is just to make it look better, nothing important. Again I apologize, I didn't have the time to add comments or explain things :(
Visit Infinity Logic for up to date programming tutorials.


Offline k3ystone

  • Recon Contributor
  • Recon Spambot
  • *
  • Posts: 415
  • Reputation: 245
    • View Profile
Re: What's wrong with my code
« Reply #5 on: March 08, 2010, 01:35:27 PM »
no worries, I understand..I just wanted to make sure he understood what you did differently to make it work.

now the question is, what are you doing fixing someone's code while you're in class?  haha

Offline Drunken.Canadian

  • Administrator
  • Administrator
  • Recon Spambot
  • *****
  • Posts: 442
  • Reputation: 270
  • ASM, C++, and C# programmer
    • View Profile
    • Infinity Logic
Re: What's wrong with my code
« Reply #6 on: March 10, 2010, 11:05:12 AM »
ummm.... not doing my work :P I was already done within 5 min of getting it xD
Visit Infinity Logic for up to date programming tutorials.


Offline k3ystone

  • Recon Contributor
  • Recon Spambot
  • *
  • Posts: 415
  • Reputation: 245
    • View Profile
Re: What's wrong with my code
« Reply #7 on: March 11, 2010, 11:26:51 AM »
haha, don't tell my professor, but I'm in class right now too.  thank goodness we're allowed to bring laptops to class, or I'd be dead of boredom by now..haha

back on topic, did you get it all figured out and working conker?  or are you still around?

Recon Networks

« on: »