[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Simple C++ code dosnt work for a noob

Posted on 2007-04-05
3
Medium Priority
?
210 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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