Solved

Simple C++ code dosnt work for a noob

Posted on 2007-04-05
3
205 Views
Last Modified: 2010-04-01
Recently decided to dive into C++ again.

 Can anyone tell me why this code doesnt work? It is supposed to validate that the user has entered a number between 2 and 15 and if not, have them re-enter it. I'm not familiar with using ! expressions. As simple as this code may seem I assure you, this isnt someones homework.  ;) I just decided to get back into it since i installed Gcc on my ubuntu. I compiled this using a comand line in ubuntu edgy.
me@mylinux:g++  myapp.cpp -o test

 Simple code:

 #include<iostream>
     using namespace std;

                      int main()
                      {
int num;
num = 0;
do
{
    cout << "Please enter a number between 2 and 15: \n";
    cin >> "num;
    }
    while (!(num >=2)&&(num<=16);
cout << "Good job!! You entered " << num << "\n";

return 0;
}

If i typed this correctly it should produce a success no matter what number you enter.

also, if anyone knows a good C++ noob book that caters to gcc or G++, i would appreciate any reccomendations.

Thanks
0
Comment
Question by:msk100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18860767
1)

     cin >> "num;

should be :

    cin >> num;

there was an " too much ...



2)

    while (!(num >=2)&&(num<=16);

the ! should be for the whole check, and not just (num >= 2). So, make that :

    while (!((num >=2)&&(num<=16)));

Note that there was also a ) missing at the end ...



I can recommend this basic C++ tutorial which should get you started :

        http://www.cplusplus.com/doc/tutorial/

Notice that on that same site you can also find a good reference of the standard libraries ...
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 18860784
Oh, and btw, it will help you a lot if you properly indent your code, like so :

    #include<iostream>
    using namespace std;

    int main()
    {
        int num;
        num = 0;

        do
        {
            cout << "Please enter a number between 2 and 15: \n";
            cin >> num;
        }
        while (!((num >=2)&&(num<=16)));

        cout << "Good job!! You entered " << num << "\n";

        return 0;
    }

It makes your code a lot better readable and easire to understand/maintain.
0
 
LVL 1

Author Comment

by:msk100
ID: 18860813
Thank you, works great now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question