0

i want it to get the input and split it up to 3 and save them as 3 strings help me please! the cout and cin at the end of the numbers function are here to help me check if it ran correctly at the moment if i do 123 + 890 it says 3 press enter to continue.

// Calculater.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int input, num1, num2, Length, op, i, result, useless; 
int numbers(string input, int Length)
{
    i = 0;
    string op;
    string num1;
    string num2;
    while (input.substr(i, 1).compare(" ") == 0 && i != Length)
    {
        i++;
        num1 = input.substr(0, i);
    }
    i = i + 2;
    op = input.substr(i, 1);
    while (input.substr(i, 1).compare(" ") == 0 && i != Length)
    {
        num2 = input.substr(0, i);
        i++;
    }
    cout << "\n" << num1 << " " << op << " " << num2;
    cin >> useless;
    return 0;
}
int main()
{
    string input;
    cout << "Calculator\nBy Matt Y-J!\n\n";
    cout << "Equation:";
    cin >> input;
    Length = input.length();
    numbers(input, Length);
    system("pause");
    return 0;
}
Matt
  • 13
  • 3
  • 6
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Apr 05 '17 at 11:34
  • i use visual studio as my ide do you know where it is? – Matt Apr 05 '17 at 11:35
  • @MatthewYeomans-Jones: it's right there in front of you, on a menu - there's a clue in the name (it says "Debug") - also check the online help for things like "step", "breakpoint", etc. – Paul R Apr 05 '17 at 11:37
  • Right click on a line, and select "set breakpoint". Then run the program - it will stop at the line, and you can single step. – Martin Bonner supports Monica Apr 05 '17 at 11:38
  • 1
    Also, start by turning `while (input.substr(i, 1).compare(" ") == 0 && i != Length) { i++; num1 = input.substr(0, i); }` into a function. You do *exactly* the same thing twice. – Martin Bonner supports Monica Apr 05 '17 at 11:39
  • 2
    ... and none of those variables need to be global - global variables make things *very* hairy as soon as you stop being able to see the whole program on one screen. – Martin Bonner supports Monica Apr 05 '17 at 11:40
  • 1
    You have more than one problem, but you can start from here: when you read input with `cin`, check what, exactly, you have read. I think you will find a surprise. – Fabio says Reinstate Monica Apr 05 '17 at 11:40
  • What you mean i looked and saw nothing @Fabio – Matt Apr 05 '17 at 11:50
  • 1
    You neednt pass length along with the string, unless you want to use only a part of the string. strings come with a length member function, which can be used instead. Your globals are all useless and confusingly named the same as the locals in your function. @MatthewYeomans-Jones I think he is saying to use `cout` to print out the content of `input`, you'll be surprised by its content. – Paul Rooney Apr 05 '17 at 11:50
  • @Martin boomer isn't that unnecessary that just complicates stuff – Matt Apr 05 '17 at 11:55
  • 1
    What do you mean by "that"? Write the separate function? or "not use globals"? The separate function is a smaller piece of code that you can write once and test standalone. Once you have got that working, you can just reuse it in the larger program. – Martin Bonner supports Monica Apr 05 '17 at 11:58
  • I do assume that the function would return the value rather than write to a global. – Martin Bonner supports Monica Apr 05 '17 at 11:59
  • 2
    I'll be clearer: your input is `123 + 890`. This string contains spaces. When you use `cin >> input;`, what do you expect the content of `input` to be? The full string? `cin` stops reading when it finds a space. Try adding `cout << "The input is: \"" << input << "\"\n";` after that line. Of course you could also inspect the content of `input` using the debugger, as Nathan told you. – Fabio says Reinstate Monica Apr 05 '17 at 12:02

1 Answers1

1

As others have pointed out in the comments there are a few problems in your code.

As the question is specifically about getting help debugging I would recommend splitting up your code into very small and simple testable bits, and confirming they behave as you expect.

This is much easier and more productive then writing lots of code making assumptions about how each bit behaves along the way, and then spending hours debugging it.

For example in your code, I understand that you want to:

  1. Input an expression (for example 123 + 890)
  2. Split that expression up into 3 strings (separated by spaces)
  3. ... other stuff

For the first task you are currently doing:

cout << "Equation:";
string input;
cin >> input;

Which seems reasonable enough. To test it, as suggested in comment, you should check that what ends up in input is as you expect. For example we could do:

cout << "Equation:";
string input;
cin >> input;
cout << "input: \"" << input << "\"" << endl;

(Note I'm printing an escaped " character either side of input to be sure exactly what's in the string). If you type 123 + 890 at the Equation: prompt, you'll see input: "123".

This probably isn't what you were expecting. As @Fabio pointed out cin used with the extraction operator (>>) stops reading at a space. To fix this there is advice here, for example you can use getline():

cout << "Equation:";
string input;
getline(cin, input);
cout << "input: \"" << input << "\"" << endl;

Which if you type 123 + 890, will return input: "123 + 890". So this bit seems to be working now. But before moving on you should probably test it behaves as you expect under foreseeable user input. What if they use tabs instead of spaces? what if they enclose their statement in quotes?

Community
  • 1
  • 1
kabdulla
  • 5,199
  • 3
  • 17
  • 30