Troubleshooting a Test Program in Visual C++ 6

In summary, the problem is that the program is not able to create as many tables as the user wants. The program is only able to create tables of integers, decimals (float) and imaginary numbers.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
I wrote a little test program to try to figure out some problems I am having in another program I'm working on.

Code:
# include <iostream>
# include <string>
using namespace std;

void ahoy(string nameEntered)
{
	cout << "Ahoy, " << nameEntered << "!" << endl;
}

int main()
{
	string name;
	cout << "enter your name: ";
	getline(cin, name);
	ahoy(name);	
	return 0;
}
I am using Visual C++ 6.
Problem 1: After I enter keyboard input I have to press enter twice. Why not once? I see this both when I am testing the program in the editor, and also when I run the .exe.
Problem 2: In the .exe, the window closes so I never see the output from the function. How can I make it pause?
I haven't dealt with C++ in a couple of years so I may have just forgotten some simple things. :redface: Thanks for your advice.
 
Physics news on Phys.org
  • #2
When I compile with gcc 3.4.4, I only have to hit enter once. I have more faith that the version compiled with gcc is correct than I do the version compiled by VS.


OTOH, there's probably a perfectly reasonable explanation for what's going on. I just haven't figured it out yet.

For problem 2, simply waiting for more input... say cin.get()... will suffice. I suspect your double-enter problem will foil that plan, though. (Though you could do it twice)


An aside: your argument to `ahoy' should probably be

const string &nameEntered

and not

string nameEntered

. What you wrote will make a brand new copy of the string object for the ahoy function to use; the alternative I suggested will pass it by reference (and states that ahoy promises not to modify it), so that copies aren't made.
 
  • #3
hmm.. I actually do need to make modifications to the string in the function of the larger program I am working on. But, if I can do a pass by reference, can I just output the original string in main() after the function has done it's work on it? I get a little confused about what I can do with strings vs. what I can do with char arrays regarding pass by reference.
 
  • #4
WRT 2) you can also run the program from a .bat shortcut using the pause command. This keeps the console window open after the program terminates so you can see the output.

For example:
Example test.bat file
Code:
@A shortcut that starts your console program and pauses so you read output
START C:\MYPROGGY\foo.exe
pause

I assume your using windows, i think you can also do the same by creating a windows shortcut and adjusting the properties options for it.

WRT 1) http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015 Maybe of help.
 
Last edited by a moderator:
  • #5
Another point:

Why not use "cin >> name;"
 
  • #6
Hurkyl said:
For problem 2, simply waiting for more input... say cin.get()... will suffice. I suspect your double-enter problem will foil that plan, though. (Though you could do it twice)

You were right. I threw in a couple of getchar(); lines and it behaved.
3trQN said:
WRT 1) See here Maybe of help.
aha! I am going down to the school labs and see if they have Visual C++ .net.
J77 said:
Another point:

Why not use "cin >> name;"
'cause Betty Lou or Zsa Zsa might want to use my program.:wink:
 
  • #7
VC++6.0 is awesome, printf is your friend..but i don't get the double enter problem when compilng your code...

getch() from conio.h is good to use if your on windows...its not a standardized library thogh.
 
  • #8
My double enter problem vanished when I recompiled in Visual C++.net in the lab. But then that brought me a few new last minute problems to work out. Fortunately I got it all sorted out before the deadline. whew!
 
  • #9
getch() is from conio.h and is nonstandard. getchar() is in stdio.h and is standard. Why would you prefer printf when using C++, though?
 
  • #10
easier to control for , for me anyways. and ... . there's no difference to using native C functions in C++..isn't iostream in C++ based on printf?
 
Last edited:
  • #11
If your using C++ you should code in C++... overloading the printf functionis very messy looking compared to overloading cout which is nice and clean.
 
  • #12
Hi all!

Im markus from finland.I have aproblem (sorry my english is not the best..)

I try to make program, whic ask numbers, and put thme in different tables. The number of tables comes from user. how can i make code, which make as many tabels as user tell?


Marksu
 
  • #13
markushwalber:

what type of numbers are being asked? just integers, decimals (float) , imaginary?

given a number how should they be formated into tables?

reply back with more details and i will assist.
 

1. How do I debug my test program in Visual C++ 6?

To debug a test program in Visual C++ 6, you can use the built-in debugger known as the "Visual Studio Debugger". This debugger allows you to step through your code, set breakpoints, and view variables to help identify and fix any errors in your program.

2. What are some common causes of test program errors in Visual C++ 6?

Some common causes of test program errors in Visual C++ 6 include syntax errors, incorrect use of variables or functions, and memory leaks. It is important to carefully review your code and use debugging tools to identify and fix these issues.

3. How can I use breakpoints to troubleshoot my test program in Visual C++ 6?

Breakpoints allow you to pause the execution of your program at a specific line of code. This can be helpful in identifying where errors are occurring and what values are being used at that point. You can set breakpoints by clicking on the left margin of your code or by using the "F9" key.

4. What is the best way to handle memory leaks in a test program in Visual C++ 6?

To handle memory leaks in a test program in Visual C++ 6, you can use the "Debug" menu and select "Start Debugging" to run your program in debug mode. This will allow you to use the "Memory" window to track and fix any memory leaks in your code.

5. How can I use the "Watch" window to troubleshoot my test program in Visual C++ 6?

The "Watch" window in Visual C++ 6 allows you to monitor the values of variables and expressions as your program is running. This can be useful in identifying any unexpected or incorrect values that may be causing errors in your test program. You can add variables or expressions to the "Watch" window by right-clicking on them and selecting "Add Watch".

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
751
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
837
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top