Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Need Help Writing C++ Programs. Please Read On:


  • Please log in to reply
22 replies to this topic

#1 KateetaK

KateetaK

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 21 September 2008 - 01:54 PM

Hi, I am very new to C++ and am trying to write a couple of programs. For the first one, I am trying to output a four-digit integer - one digit per line. I know that I have to put \n somewhere, but I can't figure out where to put them. Also, am I missing anything here other than \n? Here is what I have so far (they're outputting all on the same line):

#include <iostream>

using namespace std;

int main()
{
int num1;
int num2;
int num3;
int num4;

cout << "Enter a four digit positive integer:";
cin >> num1;
cout << num1 << endl;
cin >> num2;
cout << num2 << endl;
cin >> num3;
cout << num3 << endl;
cin >> num4;
cout << num4 << endl;
cout << endl;



return 0;
}



----Another one I am trying to write, I need the user to input an elapsed time for an event in hours, minutes, and seconds. Then I need it to output the elapsed time in seconds. How would I go about this one?

Any and all help is much appreciated!!

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:04:22 PM

Posted 21 September 2008 - 02:11 PM

You need to split the integer entered into it's digits. Look up the % operator to get yourself started :thumbsup:

The other thing is something like:

cout << "Input hours\n";
cin >> hours;
cout << "Input minnutes\n";
cin >> minnutes;
cout << "Input seconds\n";
cin >> secs;

cout << endl << endl;

cout <<"Result:\n" << (((hours * 60) + minnutes) * 60 + secs);

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 21 September 2008 - 02:21 PM

Ah thank you so much!

I am still a little confused about the first one with the \n, I don't even know if I need the \n or not (I'm stuck). I'm not sure where to use the modulus operator either. I'm also not really clear on what you mean by splitting the integer entered into its digits. (I'm a total noob at this stuff hahaha) I just know that I need the numbers to appear like this:
1
2
3
4

and not like this (which it is doing right now):
1234

Do you think you could explain that to me a bit or show me what you mean? I would realllly appreciate it.

Edited by KateetaK, 21 September 2008 - 02:28 PM.


#4 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 21 September 2008 - 02:51 PM

Actually, I don't think I know where to put this stuff into the program that I already have coded. Our teacher is horrible and I'm trying to teach everything to myself. :\

#5 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 21 September 2008 - 07:03 PM

Could anyone help me out? I'd really appreciate it.

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:22 PM

Posted 21 September 2008 - 07:28 PM

We are glad to help. I have to apologize to one of our members since he gave you the solution outright. Nobody learns anything that way.

So lets look at the program a bit differently. Here is how I would solve your problem, and I suspect, your teacher would agree with my solution.

In pseudo code, this is my solution:

Get user input as a string.
Make sure it is the correct length.
Get the first digit as a substring and print it out.
Get the second digit as a substring and print it out.
Get the third digit as a substring and print it out.
Get the fourth digit as a substring and print it out.

There is no reason to use integers at all. So my first question is, do you know how to get the user input. and print it back out to the console. The only reason to print it back to the console is to make sure that you are getting the correct input. Then, once you are sure that you are getting the proper input, can you check the length to make sure that it is four digits?

Start with the small problems first.

#7 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 21 September 2008 - 09:47 PM

Thank you so much. And I appreciate that you're helping me learn it as I'd like to not just copy something & not understand it later on. Alright, my questions may sound bad, but I'm new and the teacher is really bad. You've actually explained that alone better than anything she has tried to teach us. I apologize in advance for the ton of questions, it's just that it's killing me that I can't get this down.

So, by not using integers at all, what exactly do you mean? The "num1" stuff I added? And to get user input, do you mean cin >>? By printing back out on the console to check it, do you mean adding system("pause") before the return?

Oh, a couple more questions. To get user input as a string, do I add #include <string> under #include <iostream>? And instead of using num1, should I use cout << "static_cast<int> (don't know what goes here) = ";? And what do you mean by correct length?

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:22 PM

Posted 21 September 2008 - 10:33 PM

This is going to be brief since it is my bedtime. :thumbsup:

What I mean by the correct length is that as stated in your original problem, the input needs to be four digits long. The syntax for checking the length of a string in C++ is something along the lines of:
int str_length = my_string.length();
So you want to to something like :
if(str_length==4){
//do something;
}


Look at this page here:
String tutorial

So to check and make sure that you have the proper length string, you want to make sure that the string length is 4.

Once you have the proper length string, you use the substring method to get parts of the string. For instance, to get the first letter of a string, you would use the substring function, something like this:
String s = my_string.substr(0,0);

See what you can do with that.

#9 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 22 September 2008 - 06:34 AM

Hmm, I'm thinking this might be a little advanced for me at this point, we're only on Chapter 2 in our book & haven't learned anything like that yet. Is there a simpler way to do what I need to do? I've never seen all of this if(str_length) stuff.

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:22 PM

Posted 22 September 2008 - 07:41 AM

You have not seen variables yet? That is just a variable name.

There are hundreds of ways to do what you want, but we don't know the exact parameters for your assignment. Perhaps you should sit down and do some one on one with your instructor. I am assuming that this is a first year college course?

#11 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:06:22 PM

Posted 22 September 2008 - 09:19 AM

Stop playing with a newbie C++ coder groovicus. He wants to input as int and print it. No String classes.

Why did you remove my post with full code :thumbsup:

#12 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 22 September 2008 - 09:24 AM

Yes, this is a first year course & I'm in my fourth week right now. I'm just really confused by the professor (so is the entire class from what I hear from the other people and people who have previously taken her course, which is why I came for help online instead). I feel a little stupid because of this. So, I plan on sitting down with her but I'm not looking forward to it because she just says what things are and doesn't know how to explain why something is the way it is, I guess.

The problem itself says: "Write a program that prompts the user to input a four-digit positive integer. The program then outputs the digits of this number, one digit per line. For example, if the input is 3245, the output is:
3
2
4
5
"

I have seen variables, but not like that. I just feel stuck because the stuff she was trying to teach us last - she was using \n to make things go to different lines, so I am assuming this is how she wants it. I've been racking my brain for days with this, even before I tried any forums. I just can't get it for some reason and the assignment is due at 12:15pm today. I have no idea what to add (or remove!) so I'm completely stuck. I know that I have to be close because my program works, it just outputs the numbers wrong - all on the same line as opposed to on seperate lines.

#13 KateetaK

KateetaK
  • Topic Starter

  • Members
  • 54 posts
  • OFFLINE
  •  
  • Local time:07:22 PM

Posted 22 September 2008 - 09:25 AM

Stop playing with a newbie C++ coder groovicus. He wants to input as int and print it. No String classes.

Why did you remove my post with full code :thumbsup:



Yes, this is exactly what I am trying to do. I know it has got to be really simple and I'm just leaving something out.

#14 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:06:22 PM

Posted 22 September 2008 - 09:38 AM

Well KateetaK I posted solution here, but admin removed it.

#15 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:22 PM

Posted 22 September 2008 - 10:09 AM

The code was removed because it doesn't teach anyone anything if all they are going to do is be provided with the code. There are already tens of thousands of sites that have the necessary code. I am assuming that this person wants to be taught, not just spoon-fed. That has always been our policy.

The world is already full of stupid coders that don't understand what they are doing. We choose not to help with that process.


I feel a little stupid because of this.

There is no reason to feel stupid. Learning how to program, and learning the syntax of a particular language is very difficult at first. Just stick with it, and you will eventually get it. Your first semester is just going to be the basics.

So here is a hint. You simply need to output a newline character. So how about something like this:
cout << "\n";




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users