Solved

Simple C++ code dosnt work for a noob

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 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