Issue with terminal application program

I am -really- new to c++ and I don't have a single language I would say I "know", I know html and css for web dev but I wouldn't conceder myself a master at css and I don't count aether as being the same as a programming language. but I have played with code for almost my whole life (simple editing from childhood till now, I know how to edit things to work for the most part and have a deconstructioned understanding of how code works to some extent) I am currently taking a course for introductory programming and my teacher asked us to make basic algorithm to instruct a user on how to tie their shoes, simple yeah I know.

But I wanted to start applying my knowledge from before the class and combine it with knowledge being gained while in class because well, it just seems more fun that way. so I started to research how I could make a terminal application. I went with a plan to make a simple user input response guide for it, and have hit a wall.
I want to have the user type in exactly "yes" or "no" and than if they leave the space blank or write in something else they get a slightly dismissing response.
I tried doing a bunch of things, but I still lack the fundamentals I guess and that leaves me in the dark regarding what commands I have at my disposal to get this to work.
so I researched, learned a lot of new things while doing so, read other people trying to work out responses in terminal and how I could use certain commands, but I know that the code I have started is failing in more than one way already, I feel like once I understand why it is failing, I will feel like an idiot, and I know doing this isn't even required of me, but I really want to finish what I started, my goal every day is to be a little better than I was the day before. so
-I know it is asking a lot-
but if anyone has the time to review my code and point it its massive flaws I would appreciate it.-
-I know I am biting off more than I can handle, it is likely that there is a lot of fundamentals I should know before attempting this, I probably missed something fundamental, but, I will keep trying until I get it, mostly because having an objective and working to that objective is the way I learn best.-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//this is a program to help guide someone through tieing their shoe.

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <cctype>

using namespace std;

bool socks_on() //trigger for response continue
	{
	cout << "* Hello user, the first step to having a pair of tied shoes is to put on a sock! once you have done that, so.... did you put socks on both your feet? ";
	while (true)
		{
		string yes ="yes";
		string no = "no";
		string s;
		cin >> ws;   // skips leading whitespace
		getline(cin, s);  // can't be int, can't be switch(would not work with strings), can only be string for direct input of words, is case sense in current form
		if (s.empty()) continue; // if they just hit enter without typing go to else in int main
		getline(cin, input); {  // I can not get it to take inputs for the life of me.... still working this out
		}
			(input == "yes") {
			continue;
		if (input == "no")
			}
		cout << " A simple yes or no would suffice, have you never answered a question before? ";
		}
	}

}

int main()
{
	if (socks_on())
		cout << "good! thats a great start, next your set of shoes and slide your feet into them through the opening... are your feet inside your shoes?\n";
	else
		cout << " you are a really incapable individual aren't you?\n";
	return 0;
	cin.get();
	return 0;
}


the windows.h I put in there because I was originally coloring certain text but that I felt made the code more complex than and with the issues I have had I took it out and plan on putting it back in once I fix it up.

thank you for the time, I am okay with being made fun of and being told I am a fool, and no one needs to help me, but i would much appreciate it.
For your code, you are doing many weird things, the main problem would be the fact that cin and getline are not worth using together, either accept the limitation of using only single word inputs through your whole program, or use getline everywhere. The only hack to make it work together is to call cin.ignore() before every getline. I suppose you need getline because you want to check if the line is empty or not.

In the grit of it, you are not using the yes or no identifiers (might as well get rid of them).

On line 21 I don't see the declaration of ws???

On line 24 there is no definition of "input" you also use this throughout your function, I think you mean s, and you should not call get line twice since you only want 1 input (technically getting 3 inputs if you include the cin on 21)

You have a random scope after getline on line 24 to 25, that is very random.

On line 26 you have a comparison statement without the if

On 28 you have a if statement that is dangling without leading to any code or scope, and with a dangling end brace...

Please tell me you are using an IDE or something like notepad++ so that you can easily see that your scopes are screwed up horribly. And stick to a consistent brace format. In some IDE's you probably have a function or add on if you tried to google it, where it will automatically refactor and clean your code formatting, google "insert IDE here" code formater.

You also completely miss the fact that you need your function to return something. And you probably want an else if on the "no" check, and else to check if something else was entered after that, and return a true or false accordingly.

In main you also return 0 twice, and that means cin.get(); is never called. Also cin is not a very good way of making the program delay since you have to enter a letter then press enter, I personally use getline with a dummy string, but there are 100 ways of doing this code that may be better. Don't forget to print "press enter to continue".

Even though you told me why you put in windows.h in the tutorial, there is no excuse to why you couldn't cut it out instead of writing that sentence, and stdafx is bullshit, don't touch it I recommend you to read the article about it on this website if you still bother (and note that the speed of compilation you receive is absolutely insignificant), fstreams and cctype aren't used, this just makes you look incompetent, and you even already know you aren't using them.

You should really pay attention to your errors, but I do have to admit, errors dealing with really weird syntax and scopes are confusing, and may point to a line which doesn't make sense, but even if you don't understand an error and it seems like rubbish that is not decode-able, please consider googling the error and see what stack overflow results you get and understand what it could mean.

Overall you should really consider studying the tutorial on this website, and maybe read a book on c++ from a library, and do the the thing you do in school, which is when reading the book write notes that summarize every section in a paragraph that describes something that is complex that you think is important and worth noting because you may (and will) forget it (even if you will never read it again, noting is still good for making you remember). You can skim any parts of the book you think is boring or if you feel it is going into something you aren't ready yet (it is much like mathematics, you can't just jump into calculus), but classes, functions, and pointers are essentials, but I cannot stress how everything is kinda important in an entry level book, you may not know how something really works, but you should know when to use it.

I don't recommend going too deep into terminals, its important to understand them just like understanding old C arrays (even though there are C++ alternatives for C arrays), it shouldn't take you much time to "get" it, and I think reading more practical code from a tutorial (like lazyfoo) could help you more since it's more fun and even though it says you should be intermediate to read the tutorial since it is graphical tutorial, the tutorial explains the code quite thoroughly, and shows how real code looks like and how it works together. But note lazyfoo tutorials isn't perfect, like many tutorials. Whenever you see a tutorial, you should doubt that it isn't the best way to do things (go ahead and try and improve it!), yet appreciate their focus on readability and cleanliness.
Last edited on
tell me you are using an IDE

I am using visual studio community 2017, sorry I did not specify :( . I will be looking into IDE formating and also, I know I really have to up my organizational skills especially in regards to brackets. I also have to learn how to leave better comments

cin and getline are not worth using together, either accept the limitation of using only single word inputs through your whole program, or use getline everywhere.


I was struggling to find information on using cin and getline in conjunction, and you are right, I had a prompt for no input, but I wanted that prompt to pop up if any other input aside from yes or no was inserted, I am going to have to research this a bit more, because it isn't that I want more than one word, but my current understanding is that if I didn't use getline it would only take one letter, I am now certain that I am dead wrong but finding the information is proving to be a bit difficult for me, I started out using a switch and case if else and moved to string and getline so that I could take word inputs, no excuse, I still have more to learn... but this helps me out a LOT, and every breakdown as to how my other lines are stupid/broken helps a lot, I will reform this code and post again after I hit a point where I absolutely can't continue, or if I get it working.

stdafx is bullshit, don't touch it I recommend you to read the article about it on this website

I started a blank project in visual and it started with this in it, and gave me a failure message when I had originally deleted it so I was wondering about that, I will now read that because I was wondering what it had to offer thanks!

there is no excuse to why you couldn't cut it out instead of writing that sentence

I am still trying too burn things into my memory, and so I thought that I would keep them there so that I could remember what information I needed to pull up later to complete the application as I had wanted :( but you are 100% right, and it should all just be removed and it will.

this just makes you look incompetent

.... I am :(. This was the first attempt at writing a code from the ground up(not web related at least), I started out with a "hello world" script and started building on as I figured out how to work terminal, this I am sure led to a lot of the wonky stuff in here including the return 0 and cin.get(), I have edited code before as before mentioned, but editing code means nothing realistically speaking so I don't have sufficient information to call myself anything but incompetent.... But I do hope to change that one day.

You should really pay attention to your errors

I have, but I couldn't really figure out the cause of them :( so your breakdown really helped a lot.

Overall you should really consider studying the tutorial on this website, and maybe read a book on c++ from a library

I am taking a class at the moment and we are reading "an introduction to programming with c++" and it is a clear and easy to understand book, but I have yet to finish it, and in addition to that I have been watching youtube videos, using this site, and learn x in y minutes to try and gain a better understanding than I currently have, but as I said, I am doing this project as a fun way to force myself to understand and learn by survival.
I am going to be reading the tutorials here tonight after class.

You can skim any parts of the book you think is boring or if you feel it is going into something you aren't ready yet (it is much like mathematics, you can't just jump into calculus), but classes, functions, and pointers are essentials, but I cannot stress how everything is kinda important in an entry level book, you may not know how something really works, but you should know when to use it.

this I will take to heart.

I don't recommend going too deep into terminals, its important to understand them just like understanding old C arrays (even though there are C++ alternatives for C arrays), it shouldn't take you much time to "get" it, and I think reading more practical code from a tutorial (like lazyfoo) could help you more since it's more fun


I want to start coding arduinos and I know that they use C, so I would really enjoy learning C in the future, but this class covers C++, so I will also take this to heart. I guess I am comfortable with the terminal because I have linux on my laptop and I have a few Pi's I play with but its time to probably step away from that X/.

thank you so much for your time an I will be back here after a LOT of reflection and study.






poteto wrote:
On line 21 I don't see the declaration of ws???

<iostream> includes <istream> which is where std::ws is declared.

poteto wrote:
cin and getline are not worth using together, either accept the limitation of using only single word inputs through your whole program, or use getline everywhere. The only hack to make it work together is to call cin.ignore() before every getline. I suppose you need getline because you want to check if the line is empty or not.

Using std::ws before each call to std::getline is another way to get cin>> and getline to get along. It's much easier than ignore to use correctly. The drawback is if you care about empty lines or whitespace characters at the beginning of a line, because they will be discarded, but otherwise I only see benefits of using std::ws.

WeebKing wrote:
I want to have the user type in exactly "yes" or "no" and than if they leave the space blank or write in something else they get a slightly dismissing response.

If you are fine with the user writing spaces and tabs before the first word, and that empty lines will just keep waiting for more input (which is exactly how cin>> behaves), then you can use std::ws the way you do, otherwise you'll have to do it some other way.
Last edited on
That ws tripped me, never saw it used before. A reminder that using namespace std can confuse some people.

But from what it looks like, ws is a silver hammer to the getline/cin problem. I really haven't spent nearly enough time with C++ terminal code at all.

And weebking, does your code work?
Topic archived. No new replies allowed.