Solved

Simple C++ code dosnt work for a noob

Posted on 2007-04-05
3
204 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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

860 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