Dollars BBS | Technology

feed-icon

Main

News

Animation

Art

Comics

Films

Food

Games

Literature

Music

Personal

Sports

Technology

Random

C++ Code Snippet (20)

1 Name: Amri : 2017-07-22 17:22 ID:DFkQMvV5 [Del]

Hello, everyone! If you're reading this and you know how to code in C++, may you please help me figure out why this code snippet won't run correctly?
The user should input "2 cubits 1 palm 2 digits", and the program should print out the exact same thing but each phrase on different lines, but nothing prints out after compilation!


#include
#include
#include
using namespace std;

int main()
{
vector numbers;
vector units;
int i = 0;
string line;
getline(cin, line);
char character = line.at(i);
char space = ' ';
while(i < line.length())
{
char character = line.at(i);
string str_num = "";
string unit = "";
while(!(character == space))
{
str_num += character;
i++;
}
while(i < line.length())
{
i++;
unit += character;
}
double num = stod (str_num);
numbers.push_back(num);
units.push_back(unit);
i++;
}

for(int j = 0; j < numbers.size(); j++)
{
cout << numbers[j] << "\t" << units[j];
cout << endl;
}

return 0;
}

2 Name: Sid : 2017-07-23 20:24 ID:gilUwa78 [Del]

I only know a little c++, but a bit more java.

Why 2 while loops that check the same thing?

why have 2 char variables both named character?

I think it is with the loops, if it is/ isn't a space it keeps incrementing i, without changing the placement of the character. You never change the placement of the character, except at the first while loop. Can you not use if, or switch and case, statements for this? It would make it a lot easier.

3 Name: Amri : 2017-07-24 13:10 ID:5FXfl/ii [Del]

The point is to read in a line of input containing number lengths with their units, separated by spaces.
Example: "50 feet 9 inches"

One while loop records the number lengths into a vector, and the second while loop records the units into another vector; the placement of the while loops allows correct timing for each number in the first vector have the same subscript (or index) as its unit in the second vector.
Ex: numbers[1] contains '9'
units[1] contains 'inches'

So basically, the variables record all parts of the string input between the spaces.

4 Name: Amri : 2017-07-24 13:17 ID:5FXfl/ii [Del]

Also, right after the 'int main()', the first two lines should say 'vector\ numbers' and 'vector\ units'. For some reason, the post left out the symbols ''. I certainly hope this comment didn't leave them out, too ^_^;

5 Name: Amri : 2017-07-24 20:29 ID:5FXfl/ii [Del]

Anyway, I mainly need help figuring out what causes the program experience a run-time error, because it seems to compile and run, except it doesn't print out anything like it should.

6 Name: Ava : 2017-07-24 21:30 ID:3uul5++Q [Del]

Ill try to run it and see what I get. Is there anything else the program should do, or should it just print the phrases on different lines?

7 Post deleted by user.

8 Name: Amri : 2017-07-25 01:20 ID:5FXfl/ii [Del]

Thank you, Ava! And yes, it should print the input lengths on different lines. (^‿^)

9 Name: Amri : 2017-07-25 01:45 ID:5FXfl/ii [Del]

Actually, whoever is reading this now, can you run this code this time? It's my edited version after fixing quite a few errors:


#include
#include
#include
using namespace std;

int main()
{
string line;
cout << "Enter line of input: \n";
getline(cin, line);

vector numbers;
vector units;

int i = 0;
while(i < line.length())
{
string str_num = "";
string unit = "";
while(line.at(i) != ' ')
{
str_num += line.at(i);
i++;
}
i++;
while(line.at(i) != ' ')
{
unit += line.at(i);
i++;
}

double num = stod(str_num);
numbers.push_back(num);
units.push_back(unit);

cout << num << " " << unit << endl;
i++;
}

return 0;
}

10 Name: Ava : 2017-07-25 09:05 ID:kuimvVPm [Del]

First and foremost...where are your libraries??? You put include but no library

11 Name: Amri : 2017-07-25 10:33 ID:5FXfl/ii [Del]

Yeah those too...you know how I said the types (double, string) don't appear in the post for the vectors when I add the 'greater than' and 'less than' signs? The same thing goes to the header files; but, I did include 'iostream', 'string', and 'vector'

12 Name: Ava : 2017-07-25 10:54 ID:kuimvVPm [Del]

What IDE do you use? Some libraries' names change acording to the IDE or compiler

13 Name: Ava : 2017-07-25 10:59 ID:kuimvVPm [Del]

You may use string or cstring, for instance

14 Name: Ava : 2017-07-25 20:23 ID:vOraa8+k [Del]

I fixed it a bit but it seems the logic is flawed somewhere, as it gets an out of bounds error when trying to print the second phrase. I think you can work on my fix.

#include
#include
#include
#include
#include

using namespace std;

int main()
{
string line;
cout << "Enter line of input: \n";
getline(cin, line);

vector numbers;
vector units;

int i = 0;
while(i < line.length())
{
string str_num = "";
string unit = "";
while(line.at(i) != ' ')
{
str_num += line.at(i);
i++;
}
i++;
while(line.at(i) != ' ')
{
unit += line.at(i);
i++;
}

const char *cstr = str_num.c_str();

double num = strtod(cstr, NULL);

numbers.push_back(num);
units.push_back(unit);

cout << num << " " << unit << endl;
i++;
}

return 0;
}

15 Name: Ava : 2017-07-25 20:25 ID:vOraa8+k [Del]

The included libraries are string, cstring, iostream, vector, and cstdlib. numbers is an int vector and units a string vector. If that is not how it should go (for the vector types), you can start fixing it from there.

16 Name: Amri : 2017-07-25 22:31 ID:5FXfl/ii [Del]

Alright, thanks! I finished the assignment today, so thank you so much for your help Ava!

17 Name: Amri : 2017-07-25 22:39 ID:5FXfl/ii [Del]

And in fact, there isn't any error in your code when it says "out of bounds" because I had that situation many times when I tested that method of code. I checked with a c++ tutor, and they said that there wasn't anything wrong with it, so it was something kind of beyond the code. But my tutor gave me the function of istringstream with the header file 'sstream' and it allowed much shorter and organized code!

18 Name: Ryuugamine Dotachin : 2017-07-26 20:19 ID:ptTNKtv5 [Del]

Just saying, it said std... Lol ew

19 Name: Sid : 2017-07-26 23:25 ID:iHXDpC3z [Del]

while(i < line.length())
{
i++;
unit += character;
}
In these loops i becomes greater than the length exiting the loop the value remains and is never reset. The placement of the character never changed, since the first while loop is never run a 2nd time. This is due to i being maxed by the other loops.

But good job getting it .

20 Name: Amri : 2017-07-28 13:49 ID:5FXfl/ii [Del]

Ohhh, of course! Thanks so much Sid, I never really understood why it was out of bounds...now that I get it, it makes so much sense!

Thanks!!