Solved

Simple C++ code dosnt work for a noob

Posted on 2007-04-05
3
200 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now