1) Well I wrote my program. Saved it as a C/C++ file, and looked for compile/run, and I couldn't find it :\

2) For the code you wrote... Does that ask the user to input how many dogs/cats they have?
joshie75 wrote:
1) Well I wrote my program. Saved it as a C/C++ file, and looked for compile/run, and I couldn't find it :\

2) For the code you wrote... Does that ask the user to input how many dogs/cats they have?


Go to the PROJECT menu on the upper bar, and then go to Build and Run, or just press F9, it's quicker.

For the code I wrote, it indeed asks for input.
There is no build and run option Razz I'm gonna try a un/re-instillation.
Good luck.

Still based on your earlier program I made this:


Code:
#include <iostream>

using namespace std;

int main() {

  int n=0;                            //Number of animal types
  int numberOfAnimals;                //Final result, total number of animals
  int numberOfAnimalsPerType[n];      //An array with number of animals per type
 
  cout << "Types of animals: ";
  cin >> n;

  int i;
  for (i=0;i<n;i++)
  {
    cout << "Type " << i+1 << ": ";
    cin >> numberOfAnimalsPerType[i];
  }

 
 
  for(i=0;i<n;i++)
  {
    numberOfAnimals += numberOfAnimalsPerType[i];
  }

  cout << "Total number of animals: " << numberOfAnimals << endl;

  return 0;
}


The user enters how many animal types he has. Then he inputs how many animals he has of each type and then the program returns how many animals in total.

It could be shortened down to this too:


Code:
#include <iostream>

using namespace std;

int main() {

  int n=0;                            //Number of animal types
  int numberOfAnimals=0;                //Final result, total number of animals
 
  cout << "Types of animals: ";
  cin >> n;

  int i;
  int tempvar;
  for (i=0;i<n;i++)
  {
    cout << "Type " << i+1 << ": ";
    cin >> tempvar;
    numberOfAnimals += tempvar;
  }
 
  cout << "Total number of animals: " << numberOfAnimals << endl;

  return 0;
}


I hope I'm not confusing you too much Sad
You actually explained that quite well. As for the no compile/run... I guess when i installed it I never checked the box saying I wanted to compiler. Smile
One thing I'm confused about... iss....

Code:

for (i=0;i<n;i++)

Why two plus sings? What does that mean/stand for?

and

Code:

numberOfAnimals += tempvar;

Why the plus and equals? Redundancies confuse me Sad
The ++ increases a variable by 1, so A++ is the same thing as adding 1 to A.
The ++ is a unary operator. It takes the value of the variable and adds one to it. It is called "unary" because it only takes one operand: the variable. To go with it, there is --, which subtracts one from a variable. There is also a difference between ++i and i++. For instance, look at this code:
Code:
int a = 12;
int b = a++;
int c = ++a;
At the end of the code, A is 14 (because of the two ++ statements), B is 12, and C is 14. This can be explained because the way the code actually works for line 2 is:
Code:
int b = a;
a = a + 1;
And line 3 is:
Code:
a = a + 1;
int c = a;
The same works with --, of course.


As for the +=, this is a shorthand to reduce typing. That line could be written as:

Code:
numberOfAnimals = numberOfAnimals + tempvar;
But who wants to type all that code? Instead of typing it all, you can just use an operater-equals. You could also use /,+,-,*,%,~, etc etc. When doing division, a /= 12, it would really be: a = a / 12 (in case order of operations confuses you for that, it did for me). Makes sense?
so what you're saying is I could use it for something like this?

Code:

int fullStackofPaper;             //we'll say a full stack of paper is 20
int halfStackOfPaper = 10;   //and a half stack is 10
                                       
halfStackOfPaper += fullStackofPaper;

Wouldn't that just add half + half to get a fullstack equals 20? Or am I misunderstanding.
First off, you need semi-colons. Secondly, I think what you mean is this:
Code:
int fullStackOfPaper = 0; // Don't use initialized variables, and set to 0
int halfStackOfPaper = 10; // Full is 20, half is 10.

fullStackOfPaper += halfStackOfPaper; // 10
fullStackOfPaper += halfStackOfPaper; // 20
You could also do: halfStackOfPaper += halfStackOfPaper; Or halfStackOfPaper *= 2; Understand, though, that this will edit halfStackOfPaper.
Oh, I see now! Smile
Ok I'm quite excited right now. I'm really getting this stuff down, and I'm having a lot of fun with this now Smile
But I ran into this error on line 16, and I've looked around on the web, and in my C++ book, but couldn't find anything. Was hoping you guys knew? Here's my code...

Code:


#include <string>
using namespace std;

int main()
{
    string name = "";                                                   //Users name
    string mood = "";                                                   //Defines users mood

    cout << "Hello sir, your name is? ";
    cin >> name;                                                        //Name input
    cout << "Well then, hello " << name << ", how are you today?";
    cout << endl;
    cin >> mood;
    int moodNumber;
    mood.length() = moodNumber;      //THE ERROR IS HERE <-------
    cout << endl;
        Page1:
        switch (moodNumber) {
            case 1:
                moodNumber = 4;
                cout << "Well " << name << ", I am glad to hear that! :) ";
                cout << endl;
                break;
            case 2:
                moodNumber = 3;
                cout << "Oh no" << name << ", I am very sorry to hear that :( ";
                cout << endl;
                break;
            default:
                cout << "Please choose good or bad, my english not so premium";
                cout << endl;
                goto Page1;
        }
    return 0;
}

The error reads: "lvalue required as left operand of assignment. I'm not too good with fancy words, but I'm assuming it's telling me I can't leave the value blank... but depending on what the user types in for their mood... the value will change, HELPP Sad
To solve your problem, you would switch that statement around:
Code:
moodNumber = mood.length();
But, the thing that I _really_ see that is wrong is that goto Page1 statement. To me, this looks like it will first off, create an endless loop if they enter the wrong value, and it will also overflow the stack (I think). To be quite honest, I'm not sure how to restructure that because the program doesn't look like it would work in the first place :/
Well I'm not too bright... I meant for the page label to go here...

Code:

    cout << "Well then, hello " << name << ", how are you today?";
    cout << endl;
        Page1:
        cin >> mood;
        int moodNumber;
        moodNumber = mood.length();

So that fixed the endless loop... but still, I tried good, and I've tried bad, but both times it sends me to Page1:... :s
Try inputting something 1 or 2 characters long and see if you can find out why it didn't work Smile
Why does it do that? Sad lol
You are using the switch statement incorrectly. When you say: switch (value) { case valueTwo: break; case valueThree: break; }. You are checking to see if value is equal to valueTwo or valueThree. You are thinking (correct me if I am wrong) that you have to say all the cases, starting at 1 and going up, and then in your code you do "moodNumber = 4", which I can only assume you mean == (for comparisons), and are wanting to use it like the value within an if's parenthesis. To fix it, change the case statements to be the lengths of the string you input. Make sense?
Dude, You're a beast, thanks so much Smile
joshie, you're even helping me! I didn't know we could use cin for strings Smile
Lol Scout, me either, but being a Noobie, I was kind of just testing stuff out. Glad you caught that Very Happy
There isss one thing I've noticed about some program scripts that I've looked at... for some functions, some scripts have std:: in frotn of them, and some dont? ex;

Code:

std::cin;
std::cout;
std::endl;

But I've seen it without too like this.. (which is how I use it)

Code:

cin;
cout;
endl;

Why would people take the time to type out std:: every time? :S
Nevermind, since you use "namespace std;" that tells the program that cin, cout, etc are std Smile yay me lol
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement