12 Replies - 729 Views - Last Post: 22 September 2010 - 08:47 PM Rate Topic: -----

#1 shreddingdiego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 10

Help with looping

Posted 22 September 2010 - 06:43 PM

Hello everybody I'm new here so hello and I need help with a looping assignment. This is for my first C++ so any help would be greatly appreciated.
The problems is -- Write a program which converts inches into feet and inches by using loop(s) (while/do-while/for statement(s)). Do NOT use division (/) or mod (%) operations. Receive user's input (original inches) from keyboard.

You can terminate the program after one session. If you work more so that the program repeats the session until the user signals with -1, you will receive a 1 point EXTRA CREDIT --

The output is supposed to look like this -- WELCOME TO THE INCH CONVERTER! --

Enter the original inches: 71
** Equivalent feet/inches: 5 feet, 11 inches **

----- THANK YOU. GOOD BYE! -----


This is my code so far

#include <iostream>
using namespace std;

int main()
{
    cout << "----- WELCOME TO THE INCH CONVERTER! ----- " << endl;

    int inches;


    cout << "Enter the original inches: ";
    cin >> inches;

    int counter;
    counter = 0;

    while (counter < 12)
    {
        if (inches >= 12)
            inches = inches - 12;
            
        //counter ++;
        counter = counter + 2;
    }

    cout << "** Equivalent feet/inches: " <<  foot << ", " << inches << endl;
    cout << "----- THANK YOU.  GOOD BYE! ----- " <<endl;

    //End of the program
    system("pause");
    
    return 0;
}


I am able to display the inches right but for some reason i cant get the feet displayed. Thanks any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with looping

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with looping

Posted 22 September 2010 - 06:48 PM

Quote

I am able to display the inches right but for some reason i cant get the feet displayed.
So what actually happens?
Was This Post Helpful? 0
  • +
  • -

#3 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 50
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Help with looping

Posted 22 September 2010 - 06:51 PM

The variable foot is never declared, initialized, or actually used - however you try to output it...
Was This Post Helpful? 0
  • +
  • -

#4 shreddingdiego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 10

Re: Help with looping

Posted 22 September 2010 - 06:52 PM

Well my output is if I enter 71 inches as the original inches i dont get a feet output just the inches output like is displayed below.
-- WELCOME TO THE INCH CONVERTER! --

Enter the original inches: 71
** Equivalent feet/inches: feet, 11 inches **

----- THANK YOU. GOOD BYE! -----

The number 5 doesn't come out where its supposed to.
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with looping

Posted 22 September 2010 - 06:57 PM

Here's what happens when I try to compile your code with MSVC:

yourcode.cpp(26) : error C2065: 'foot' : undeclared identifier



With g++:

yourcode.cpp: In function 'int main()':
yourcode.cpp:26:47: error: 'foot' was not declared in this scope
yourcode.cpp:30:19: error: 'system' was not declared in this scope



What compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#6 shreddingdiego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 10

Re: Help with looping

Posted 22 September 2010 - 06:58 PM

ok so if I declare a foot variable and initialize it how can i get to display the number of feet without using any division. Because I tried already declaring and initializing it but the feet comes out as the wrong answer.
Was This Post Helpful? 0
  • +
  • -

#7 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 145
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Help with looping

Posted 22 September 2010 - 07:00 PM

I always recommend not using
system("pause");
it's bad for many reasons. Instead use
cin.get();
and that sometimes won't work if there is already input in the buffer then use
cin.get();
cin.ignore();

This post has been edited by Alex6788: 22 September 2010 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Help with looping

Posted 22 September 2010 - 07:01 PM

Just a tip: conversion from feet to inches can be done with a simple
feet = inches * 0.0833333333;


Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with looping

Posted 22 September 2010 - 07:04 PM

Quote

ok so if I declare a foot variable and initialize it how can i get to display the number of feet without using any division.
How were you able to get the correct number of inches show up, without division?
Was This Post Helpful? 0
  • +
  • -

#10 shreddingdiego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 10

Re: Help with looping

Posted 22 September 2010 - 07:08 PM

at the moment i am using visual studio 2010 and your getting that error cause the foot variable wasnt declared yet i missed that still though ive declared it but honestly have no idea where or how to use that to get the right formula for the foot sorry im sure this is a complete noob question but i really do appreciate all your help

that is supposed to be the twist to the problem our professor doesnt want us to use division or mod operators to get the conversion she wants us to use loops and she told us all we were going to need was adding or subtracting and i guess thats the part that is really confusing me.
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with looping

Posted 22 September 2010 - 07:11 PM

Quote

at the moment i am using visual studio 2010 and your getting that error cause the foot variable wasnt declared yet i missed that
What do you mean you "missed" that? If you have an error in your code, you won't have a program to run. Yet in your posts, you told us you were running the program and getting unexpected output. Huh?

How did you construct your code? I mean: while (counter < 12) . What is the purpose of counter? Why 12?

counter = counter + 2; Why are you adding 2? Why not 3? Or 1?

If you're having trouble with the program, go through this exercise. Using pen and paper, convert from inches to feet + inches, without dividing. For example, if I gave you 100 inches, convert to feet and inches. Write down all the steps you took, and show it to us.

This post has been edited by Oler1s: 22 September 2010 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#12 anonymouscodder  Icon User is offline

  • member icon

Reputation: 122
  • View blog
  • Posts: 701
  • Joined: 01-January 10

Re: Help with looping

Posted 22 September 2010 - 07:33 PM

Quite easy to fix, but what a mess eh?

#include <iostream>
using namespace std;

int main()
{
    cout << "----- WELCOME TO THE INCH CONVERTER! ----- " << endl;

    int inches;


    cout << "Enter the original inches: ";
    cin >> inches;

    int foot; //Replaced EVERY "counter" for "foot"
    foot = 0;

    while (inches >= 12) //Replaced for (inches >= 12) (previously your if condition)
    {
        //Remove if statement (but not the body)
            inches = inches - 12;

        foot ++; //Uncommented this line
        //Removed this one: foot = foot + 2;
    }

    cout << "** Equivalent feet/inches: " <<  foot << ", " << inches << endl;
    cout << "----- THANK YOU.  GOOD BYE! ----- " <<endl;

    system("pause");

    return 0;
}


Study more.
Was This Post Helpful? 1
  • +
  • -

#13 shreddingdiego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 10

Re: Help with looping

Posted 22 September 2010 - 08:47 PM

ok thank you very much yeah if you cant tell im new to coding but im trying, but i got the code correct i had a couple of things wrong well a lot of things but thank you all very much for your help i can tell you guys really know what you are doing and if you are interested what the code was to that problem well here it is
#include <iostream>
using namespace std;

int main()
{
    cout << "----- WELCOME TO THE INCH CONVERTER! ----- " << endl;

    int inches; //variable of inches the user will input.
    int feet = 0; // feet is equal to zero because we will increment by 1 in the loop.

    cout << "Enter the original inches: ";
    cin >> inches;

    while (inches >= 12) // loop if the inches is bigger than 12 then the loop will start.
    {
            inches = inches - 12; // upadate the inches.
            
        feet++; // increment variable feet by 1.
    }


    cout << "** Equivalent feet/inches: " << feet << ", " << inches << endl;
    cout << "----- THANK YOU.  GOOD BYE! ----- " <<endl;

    //End of the program
    system("pause");
    
    return 0;
} 


oops i didnt see that by anonymouscodder but thank you so much and yes i know i need a lot of studying but thanks a lot for your help.

This post has been edited by JackOfAllTrades: 23 September 2010 - 04:04 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1