0

I am almost done with the code i just need to figure out how to make the user input values for character and the height of the triangle using cout and cin thanks this is all my code hard coded.

I feel like i worded it wrong basically the program is supposed to draw a triangle using the function drawline i created below, when i compile and run it asks me to enter a user choice if i enter 1 it runs the code in the if (userChoice == 1){} basically i want a cin and cout code structure that allows them to input their values for lineLength and displayChar.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void drawLine (int lineLength, char displayChar);
void placePoint (int lineLength) ;

int main()
{
    int userChoice = 0;
    cout << "**********************************" << endl;
    cout << "* 1 - DrawTriangle *" << endl;
    cout << "* 2 - Plot Sine graph *" << endl;
    cout << "* 3 - Exit *" << endl;
    cout << "Enter a selection, please: " << endl;
    cin >> userChoice;

    int x,y,t =0;
    char displayChar = ' ';
    int lineLength = 0;
    double sinVal= 0.00;
    double rad = 0.00;
    int plotPoint = 0;

    if (userChoice == 1)
        for (int x=1; x <= lineLength; x=x+1) {
            drawLine ( x, displayChar);
        }//end for

    for (int y=lineLength-1; y >= 1; y=y-1) {
        drawLine ( y, displayChar );
    }//end for
}//end main at this point.

void drawLine (int lineLength, char displayChar) 
{
    for (int x=1; x <= lineLength; x=x+1) {
        cout << displayChar;
    }
    cout << endl;

    for (int y=y-1; y >= 1; y=y-1) {
        cout << displayChar;
    }
    cout << endl;
} //end drawline
Deele Fa
  • 3
  • 6
  • Just ask the user with `cout` and input with `cin`? – Some programmer dude Oct 12 '12 at 08:09
  • thanks i tried to do it directly using the variables lineLength and displayChar if you saw the number of characters it gave me you would have laughed your head off it just kept iterating – Deele Fa Oct 12 '12 at 08:11
  • this is exactly what i did @JoachimPileborg cout << Enter a value for the length of the triangle << endl; cin >> lineLength; cout << Choose a character to use: << endl; cin >> displayChar; – Deele Fa Oct 12 '12 at 08:12

3 Answers3

0

The problem is that cin is a stream (see reference document), so you cannot just stream the value into userChoice as it is an int. Instead you need to use a string:

string response;
cin >> response;

Then you need to parse the string to get the int, using one of the methods in this SO question, like strtol.

Similar question on reading ints here: How to properly read and parse a string of integers from stdin C++

Alternatively, just use the string response for your comparison:

if(response == '1') {
    //...
}
Community
  • 1
  • 1
Phil H
  • 19,928
  • 7
  • 68
  • 105
  • thanks a lot i know that i am not trying to stream directly to userChoice i am trying to let it take in the lineLength and displayChar variable to determine character and height – Deele Fa Oct 12 '12 at 08:16
  • The offending line is `cin >> userChoice;` - userChoice was declared as an int. – Phil H Oct 12 '12 at 08:21
  • Forgive me if I'm wrong - I thought << is overloaded and it works based on the type of the variable, and I believe cin in this case will return an integer. – Raj Oct 12 '12 at 08:25
  • yes @Raj >> actually returns a variable i just read the documentation – Deele Fa Oct 12 '12 at 08:32
0
for (int y=y-1; y >= 1; y=y-1)

initializes y to an indeterminate value. This means that the loop will have a random, possibly very long, duration.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
-1

You can't use cin to set an integer. Because cin is a stream, you can use it to set a string. From there you can convert the string into an integer using atoi. You can look that up on cplusplus.com for more details.

Your implementation should be something like this:

string userChoiceString;
cin >> userChoiceString;
userChoice = atoi(userChoiceString.c_str());
Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
innospark
  • 778
  • 1
  • 5
  • 8
  • I feel like i worded it wrong basically the program is supposed to draw a triangle using the function drawline i created below, when i compile and run it asks me to enter a user choice if i enter 1 it runs the code in the if (userChoice == 1){} basically i want a cin and cout code structure that allows them to input their values for lineLength and displayChar. – Deele Fa Oct 12 '12 at 08:35
  • Thanks for editing my response, I'm still new to stack overflow & I'm trying to get the hang of how this site & community works :) – innospark Oct 14 '12 at 02:29