while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbiemom
    New Member
    • Feb 2007
    • 10

    #1

    while loop

    I am totally confused I cant even get started and my assignment is getting later by the day. We have to use a while loop to do several things. This is the source code I have so far , can someone tell me where to go from here
    This is me trying to print odd numbers from 2 numbers input by the user. I also have to sum and print the even numbers. I have to print squares of numbers between 1 and 20.
    Code:
    // Source Code
    # include <iostream>
    
    using namespace std;
    
    int main
    
    int firstnum;
    int 2ndnum;
    int odds;
    int sumevens = 0;
    int counter;
    string uppercaseLetters;
    
    sum = 0
    
    cout << " Please input 2 integers,make the 2nd number larger than the first number"
    cin >>firstnum>>2ndnum
    cout << " The first number entered is :"<< first num;
    cout<< " the 2nd numberentered is:" << second num;
    cout << endl;
    while ((firstnum < second num)&& firstnum % 2 = 0)) 
           cout << firstnum++
    else if((firstnum < second num)&& firstnum % 2 = 1)
            cout<< sumevens
            sumevens= sumevens + num
    
    //Do I need to use a counter. How do I get it to go on to the next odd integer?
    Last edited by newbiemom; Feb 28 '07, 05:39 AM. Reason: spelling
  • dfound
    New Member
    • Jan 2007
    • 52

    #2
    Sorry , can't understand your question.You are saying that there are only 2 numbers.Then why do you want to go to next odd number.And what exactly do you want the program to do. Is it to print odd number from the 2 numbers taken as input
    or is it to print odd numbers after the 2 numbers..?????

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      There are many things syntactically incorrect with your program. First, there are several semicolons missing, especially around the while loop. Next, 2ndnum is an illegal identifier - variable names cannot begin with a number in C++. Also, your int main has no parentheses after it and no brackets - thus, the rest of your code is not even interpreted as being a part of main(). Fix these, and we can work with you from there.

      Comment

      • newbiemom
        New Member
        • Feb 2007
        • 10

        #4
        [QUOTE=dfound]Sorry , can't understand your question.You are saying that there are only 2 numbers.Then why do you want to go to next odd number.And what exactly do you want the program to do. Is it to print odd number from the 2 numbers taken as input
        or is it to print odd numbers after the 2 numbers..

        yes, the user must input 2 numbers and then the program must print the odd numbers between those 2 numbers input by the user . Then the program must print and add together all even numbers.

        Comment

        • newbiemom
          New Member
          • Feb 2007
          • 10

          #5
          [QUOTE=newbiemom]
          Originally posted by dfound
          Sorry , can't understand your question.You are saying that there are only 2 numbers.Then why do you want to go to next odd number.And what exactly do you want the program to do. Is it to print odd number from the 2 numbers taken as input
          or is it to print odd numbers after the 2 numbers..

          yes, the user must input 2 numbers and then the program must print the odd numbers between those 2 numbers input by the user . Then the program must print and add together all even numbers.
          souce code reposted
          Code:
          # include <iostream>
          # include <cmath> // for finding squares
          using namespace std;
          
          int main;
          
             int firstNum;
             int secondNum;
             int odds;
             int sumevens = 0;
             int counter;
             string uppercaseLetters;
          
             cout << " Please input 2 integers,make the 2nd number larger than the first number";
             cin >>firstNum>>secondNum;
             cout << " The first number entered is :"<< firstNum;
             cout<< " the 2nd number entered is:" << secondnum;
             cout << endl;
             while ((firstNum < secondNum)&& firstnum % 2 = 1)) // while loop to print odd #'s
             {
                cout << firstnum++
                else if((firstnum < second num)&& firstnum % 2 = 0) 
                cout<< sumevens
                sumevens= sumevens + num;
             }

          // what is a constructor or destructor, I'm getting an error message for that in lines 19 and 20

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            You still haven't declared your main() function properly. What you have is

            Code:
            int main;
            It should be

            Code:
            int main() {
               // ... rest of the code here ...
               return 0;
            }
            Also, you have an else if in the middle of your while loop - with no if statement. else ifs pair up with ifs, not while loops.

            Comment

            • newbiemom
              New Member
              • Feb 2007
              • 10

              #7
              Originally posted by Ganon11
              You still haven't declared your main() function properly. What you have is

              Code:
              int main;
              It should be

              Code:
              int main() {
                 // ... rest of the code here ...
                 return 0;
              }
              Also, you have an else if in the middle of your while loop - with no if statement. else ifs pair up with ifs, not while loops.
              Reposting code

              Code:
              # include <iostream>
              # include <cmath>
               
              using namespace std;
              
              int main()
              {
                        int firstNum; // variable to store first numner entered by the user
                        int secondNum; // variable to store second number entered by the user   
                        int numOdd = 0; // variable to store odd number 
                        int sumEvens = 0; //variable to store sum of odd numbers
                        int counter;
                        
              
              
              
                cout << " Please input 2 integers,make the 2nd number larger than the first number";
                cin >> firstNum >> secondNum;
                cout << " The first number entered is :"<< firstNum;
                cout<< " the second number entered is:" << secondNum;
                cout << endl;
                while (firstNum < secondNum)
               {     
               if (firstNum % 2 == 1)                       // to get computer to find odd #s
                   cin>> numOdd;                            // between 1st and 2nd number
                   cout<<" the odd numbers are:"<< numOdd <<  
                   numOdd= firstNum++;
               else
                    cout<< "That was an even number.";        // if even do something else
                        
                      
               }       
              //  no match for operator= in(+(+std::operator<<[with_traits<char>](((std:basic_0
                       // this is the message I am getting for line 27 , What does it mean
                    Return 0;

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Code:
                 if (firstNum % 2 == 1)                       // to get computer to find odd #s
                     cin>> numOdd;                            // between 1st and 2nd number
                     cout<<" the odd numbers are:"<< numOdd <<  
                     numOdd= firstNum++;
                Your cout statement ends in a <<, so the compiler is looking for something else to output - but the next statement is an assignment statement. If you are not outputting anything further, you should get rid of the << and put a semicolon after numOdd, or at least output an endl; there.

                Comment

                • newbiemom
                  New Member
                  • Feb 2007
                  • 10

                  #9
                  Originally posted by Ganon11
                  Code:
                   if (firstNum % 2 == 1)                       // to get computer to find odd #s
                       cin>> numOdd;                            // between 1st and 2nd number
                       cout<<" the odd numbers are:"<< numOdd <<  
                       numOdd= firstNum++;
                  Your cout statement ends in a <<, so the compiler is looking for something else to output - but the next statement is an assignment statement. If you are not outputting anything further, you should get rid of the << and put a semicolon after numOdd, or at least output an endl; there.
                  Code:
                   while (firstNum < secondNum)
                   {     
                   if (firstNum % 2 == 1)                       // to get computer to find odd #s
                       cin>> numOdd;                            // between 1st and 2nd number
                       cout<<" the odd numbers are:"<< numOdd; 
                   }
                   else cout<< "That was an even number.";        // if even do something else
                            
                          
                          //  no match for operator= in(+(+std::operator<<[with_traits<char>](((std:basic_0
                           // this is the message I am getting for line 27 , What does it mean
                        system("pause");   
                        Return 0;  
                  }
                  while loop still is not working.
                  compilers is saying a ; and primary -expression expected before else
                  Also do you think this will print the odd numbers and sum them or do i need to doo it differently. I also am going to need to sum the even numbers. I have looked at the examples in my text , but it is stil puzzling
                  Last edited by RedSon; Mar 2 '07, 03:40 PM. Reason: code tags

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    It appears that you are having much bigger problems then with this one program. You need to read some very basic literature on the structure of a C/C++ program and how to make the correct syntax.

                    Please look at this website's tutorials:



                    Just start at the top of the list and work your way down. They will be able to help you much more then we can one line at a time.

                    Comment

                    • Extremist
                      New Member
                      • Jan 2007
                      • 94

                      #11
                      Hi there, I've looked at your code:
                      There is no 'in' operator - you can't say if something in somethingelse
                      Then, you are never testing if the while is still valid:
                      This is the basic syntax for a while- loop:
                      Code:
                      #include <string>
                      using namespace std;
                      int main()
                      {    
                          int num1,num2; 
                              
                          while(num1 < num2)   // Just as you started it
                             {
                                   if(num1 % 2 == 0)
                                         cout << "This is an even number" << endl;
                                   else
                                        cout << "This is an odd number" << endl;
                                   cout << "Enter a new number: " ;
                                   cin >> num2;   // Here num2 gets a new value, which enables the while to test if it is smaller than the first one, or to exit.  
                                  // always remeber a follow-up 
                             }
                          return 0;
                      }
                      Go read the basic syntax for C++ - Your problem is very basic.
                      But I can't really see what you are trying to do

                      Comment

                      Working...